Json对象和string之间的转换

JSON-----------JavaScript Object Notation

是一种轻量级的数据交换格式,因为采用完全独立于语言文本格式【易于人阅读和编写,所以一般使用JSON作为网络传输的格式,成为理想的数据交换“语言”。

它基于JavaScript(Standard ECMA-262 3rd Edition - December 1999)的一个子集,使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)【易于机器解析和生成】

Json可以分为两个部分:

Json Object(A collection of name/value pairs):{key-value,key-value}

{"state_code":101,"data":{"goods_name":"M1","goods_number":1}}

JSON Array (An ordered list of values):[{},{}]

[{"type": 1, "resource": "urlxxxx", "serverlist": "urlyyyy"}, {"type": 2, "resource": "urlxxxx2", "serverlist": "urlyyyy2"}]

value也可以jsonObject和JsonArray

 

目录

C# LitJson库

Java——org.json.JSONObject

Objective-C——通过NSDictionary创建,NSData转换,生成NSString



C# LitJson库

文本格式:{"state_code":101,"data":{"goods_name":"M1","goods_number":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;

Json对象转成string对象——JsonData.ToJson:

string json2 = data2.ToJson(); 

string对象转成Json对象——JsonMapper.ToObject

JsonData jsonData2 = JsonMapper.ToObject(json2);
Debug.Log(jsonData2["state_code"] + "    " + data2["data"]["goods_name"]);

其他对象转为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);

string对象(json格式的)转回其他对象——JsonMapper.ToObject<其他对象>

Player player2 = JsonMapper.ToObject(json);

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:[{Json Object},{Json Object},{Json Object}]

JSONArray array = new JSONArray(str);
for(int i=0;i {
     JSONObject job = array.getJSONObject(i);

     // 获取某个值

     job.get("key")

}


Objective-C——通过NSDictionary创建,NSData转换,生成NSString

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);
}

JSON Array 的格式的例子:

 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); 
            }
}

 

你可能感兴趣的:(大杂烩)