C#中LitJson转Json使用方法与坑

LitJson  本文 C#语言来介绍使用JSON,可以在C#应用程序,web程序,还有Unity3d C#脚本中使用。

其中有借鉴 http://www.cnblogs.com/peiandsky/archive/2012/04/20/2459219.html


第一步:先去下载LitJson并导入当前的项目。

可以在百度网盘下载http://pan.baidu.com/s/1mhEZpNy

不多说,直接上代码


using LitJson;


public class Player
{
    public string name;
    public int age;
    public string sex;
    public double a;
}


public class Player2
{
    public string name;
    public int age;
    public int score;
    public string  birth;


}

坑:LitJson 不支持 float 类型数据

 支持的数据类型有下面几种:

public JsonData(bool boolean);  
public JsonData(double number);  
public JsonData(int number);  
public JsonData(long number);  
public JsonData(object obj);  
public JsonData(string str);  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

参考:

  • JsonException: Max allowed object depth reached while trying to export from type System.Single



    void Start()
    {
        
        //使用JsonData来处理生成json: { "name":"peiandsky","age":28,"sex":"male"}
        JsonData data = new LitJson.JsonData();
        data["name"] = "小李";
        data["age"] = 28;
        data["sex"] = "male";
        string json = data.ToJson();
        //.对象中嵌套对象:{"name":"peiandsky","info":{"sex":"male","age":28}}
        JsonData data2 = new LitJson.JsonData();
        data2["name"] = "peiandsky";
        data2["info"] = new JsonData();
        data2["info"]["sex"] = "male";
        data2["info"]["age"] = 28;
        string json2 = data2.ToJson();
        //将上述两种方式的Json解析到JsonData
        JsonData jsonData2 = JsonMapper.ToObject(json2);
        Debug.Log(jsonData2["name"] + "   " + data2["info"]["sex"]);
       
        // 使用JsonMapper来处理Json
        Player player = new Player();
        player.name = "peiandsky";
        player.age = 23;
        player.sex = "male";
        player.a = 2.5;
        string json4 = JsonMapper.ToJson(player);
        //解析Player的Json
        Debug.Log(json4);
        Player player2 = JsonMapper.ToObject(json4);
        Debug.Log(player2.age);
        Debug.Log( player2.name);


    
        // 使用最原始的方式生成Json
        //将数组转换成json:["one", "two", "three", "four"]
        JsonWriter writer = new JsonWriter();
        writer.WriteArrayStart();
        writer.Write("one");
        writer.Write("two");
        writer.Write("three");
        writer.Write("four");
        writer.WriteArrayEnd();
        Debug.Log(writer);
       
        //将复合对象转换成json字符串:{"book":{"title":"android game!","author":"pei","bookdetail":{"pages":429,"about":null}}}
        //这种方式非常不方便,不建议使用。
        JsonWriter writer2 = new JsonWriter();
        writer2.WriteObjectStart();
        writer2.WritePropertyName("book");
        writer2.WriteObjectStart();
        writer2.WritePropertyName("title");
        writer2.Write("android game!");
        writer2.WritePropertyName("author");
        writer2.Write("pei");
        writer2.WritePropertyName("bookdetail");


        writer2.WriteObjectStart();
        writer2.WritePropertyName("pages");
        writer2.Write(429);
        writer2.WritePropertyName("about");
        writer2.Write(null);
        writer2.WriteObjectEnd();


        writer2.WriteObjectEnd();


        writer2.WriteObjectEnd();
        Debug.Log(writer2.ToString ());
         
        //在使用LitJson中,建议使用JsonData,JsonMapper来处理Json的编码和解析。
        Player2 play2 = new global::Player2();
        play2.name = "黄玉磊";
        play2.age = 15;
        play2.score = 25;
        play2.birth = "谁用谁知道!";
       Player2[] P_array = { play2, play2, play2 };
        string json_array = JsonMapper.ToJson(P_array);
        Debug.Log(json_array);


        JsonData pa1 = JsonMapper.ToObject(json_array);
        Debug.Log(pa1.IsArray +"      "+ pa1.Count);


        for (int i = 0; i < pa1.Count; i++)
        {
            Debug.Log(pa1[i]["name"]+"-"+pa1[i]["age"]+"-"+pa1[i]["score"]+"-"+pa1[i]["birth"]);
            int age = int.Parse(pa1[i]["age"].ToString ());
            Debug.Log(age);
        }


        
    }

你可能感兴趣的:(Unity,插件使用)