目录
JSON-----------JavaScript Object Notation
string转JsonArray以及获取第几个JsonObject:
Objective-C——先创建NSDictionary,然后通过NSData转换,再生成NSString
JSON-----------JavaScript Object Notation
是一种轻量级的数据交换格式,因为采用完全独立于语言的文本格式【易于人阅读和编写】,所以一般使用JSON作为网络传输的格式,成为理想的数据交换“语言”。
它基于JavaScript(Standard ECMA-262 3rd Edition - December 1999)的一个子集
使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)
【易于机器解析和生成】
Json可以分为两个部分:
1)Json Object(A collection of name/value pairs):{}
具体:{key1:value1,key2:value2,……}
{"state_code":101,"data":{"goods_name":"M1","goods_number":1}}
2)Json Array (An ordered list of values):[{},{}]
[{"type": 1, "resource": "urlxxxx", "serverlist": "urlyyyy"}, {"type": 2, "resource": "urlxxxx2", "serverlist": "urlyyyy2"}]
value也可以Json Object和Json Array
C# LitJson库
文本格式:{"state_code":101,"data":{"goods_name":"M1","goods_number":1}}
1)生成Json对象——使用JsonData类:
JsonData data2 = new JsonData(); data2["state_code"] = 101; data2["data"] = new JsonData(); data2["data"]["goods_name"] = "M1"; data2["data"]["goods_number"] = 1;
2)Json对象转成string对象——使用JsonData.ToJson:
string json2 = data2.ToJson();
3)string对象转成Json对象——使用JsonMapper.ToObject
JsonData jsonData2 = JsonMapper.ToObject(json2); Debug.Log(jsonData2["state_code"] + " " + data2["data"]["goods_name"]);
4)其他对象转为string对象(json格式的)——JsonMapper.ToJson
Player player = new Player(); player.name = "peiandsky"; player.age = 23; player.sex = "male"; string json=JsonMapper.ToJson(player); Debug.Log(json);
5)string对象(json格式的)转回其他对象——JsonMapper.ToObject<其他对象>
Player player2 = JsonMapper.ToObject<Player>(json);
6)JSON Array 的转化例子:
Person[] p_array = { p,p,p}; string json_array=JsonMapper.ToJson(p_array); Debug.Log(json_array) JsonData pa = JsonMapper.ToObject(json_array);
Java——使用org.json.JSONObject
例子:
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class UnityChannelBriage {
public static void getHt(){
JSONObject object = new JSONObject();
try{
object.put("state_code",101);
JSONObject objectData = new JSONObject();
objectData.put("goods_name","M1");
objectData.put("goods_number",2);
objectData.put("port","");
object.put("data",objectData);
String paraJson = object.toString();
}catch(JSONException e){
System.out.println("异常");
}
}
}
string转JsonArray以及获取第几个JsonObject:
str:[{k1:v1},{k2:v2},{k3:v3}]
JSONArray array = new JSONArray(str);
for(int i=0;i<array.length();i++)
{
JSONObject job = array.getJSONObject(i);// 获取某个值
job.get("key")
}
Objective-C——先创建NSDictionary,然后通过NSData转换,再生成NSString
例子1:goodsInfo是一个可以装成JSON Array的array。
- (void)rGoodsListSuccessedGoodsInfo:(NSArray *)goodsInfo {
NSLog(@"GoodsListSuccessedGoodsInfo - :%@", goodsInfo);
NSDictionary *callbackDict = @{@"state_code":[NSNumber numberWithInt:101],
@"data":goodsInfo
};
NSData *callbackData = [NSJSONSerialization dataWithJSONObject:callbackDict options:NSJSONWritingPrettyPrinted error:nil];
NSString *callbackString = [[NSString alloc] initWithData:callbackData encoding:NSUTF8StringEncoding];
NSLog(@"%@",callbackString);
}
例子2:getUrl上存放是json array格式的文本
NSData *gethostData = [NSData dataWithContentsOfURL:getUrl];
NSArray *gethostArry = [NSJSONSerialization JSONObjectWithData:gethostData options:NSJSONReadingMutableContainers error:nil];
for (NSDictionary *dic in gethostArry) {
NSLog(@"%@",dic);
if ([dic[@"type"] isEqualToNumber:[NSNumber numberWithInt:1]])
{
NSDictionary *callbackDict = @{@"state_code":[NSNumber numberWithInt:101],
@"data":@{@"goods_desp":@"none",
@"goods_number":dic[@"goods_number"],
@"goods_name":dic[@"goods_name"],
}
};
NSData *callbackData = [NSJSONSerialization dataWithJSONObject:callbackDict options:NSJSONWritingPrettyPrinted error:nil];
NSString *callbackString = [[NSString alloc] initWithData:callbackData encoding:NSUTF8StringEncoding];
NSLog(@"%@",callbackString);
}
}
各种编程语言的字符串,字符串相加,类型转换
GO
是byte的数组。底层是byte,byte是一个字节。
C++
string类的实现 stl中的类。
C#
String类
OC
nsstring