c#中的json数据

JSON数据

数据传输的语言,用于前后端数据交互的语言,注意xml的区别。

json和xml的区别

xml:可扩展标记语言,是一种用于标记电子文件使其具有结构性的标记语言

json:(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式。

二者区别:

它们都是一种数据交换格式。

1,xml是重量级的,json是轻量级的。

2,xml在传输过程中比较占带宽,json占带宽少,易于压缩。

3,xml和json都用在项目交互下,xml多用于做配置文件,json用于数据交互。

4,json可用jackson,gson等方法解析,xml可用dom,sax,demo4j等方式解析。

1.json基础

javascript object notation JS 对象

1.1定义

JSON(JavaScript Object Notation, JS对象简谱)是一种轻量级的数据交换格式。它基于 ECMAScript(European Computer Manufacturers Association, 欧洲计算机协会制定的js规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。

1.2json的数据格式

数字型:short int long float double

字符型: string char

布尔型: true false

null: null

数组型:

对象型:

1.3json的符号含义

[ ] 表示数组

{ } 表示对象或者字典

1.4序列化

c#----json
​
结果是string

1.5反序列化

json的使用

json---c#的

1.6json的格式

大括号包裹

键值对使用 逗号隔开

键值对使用冒号隔开:

  1. {"ID":102,
    "Name":"jame",
    "Age":18,
    "IsStudent":false,
    "obj":null
    }
    最后键值对没有逗号

2.excel怎么变为json

excel----保存为csv---在线工具转为json

注意:如果excel的内容是///英文输入法的逗号//,会包错,在线转换json的工具有bug。

1.编辑excel文件

2.另存为CSV格式

3.打开并复制csv

4.在线转换csv--json

5.检验一下是否是正确的json格式

[{
        "ID": 1,
        "Name": "大刀",
        "Descript": "可以涮敌人两个Hp",
        "Level": 3,
        "price": 3
    },
    {
        "ID": 2,
        "Name": "皇荒杖",
        "Descript": "可以积分,和打怪",
        "Level": 2,
        "price": 2
    },
    {
        "ID": 3,
        "Name": "雨霖铃",
        "Descript": "迷惑一分钟(暂停)",
        "Level": 3,
        "price": 2
    },
    {
        "ID": 4,
        "Name": "补气丹",
        "Descript": "可以补充1个HP",
        "Level": 1,
        "price": 1
    }
]

6.保存为json文件

7.放置在resources文件夹的json的文件夹

3.json的工具

www.bejson.com

4.unity的内置转换json的工具

最外层结构必须是对象结构

类名:JsonUtillity

方法:

ToJson(对象)

formJson<类名>()

4.1c#--转为json(序列化)

1.写一个类,里面有属性、

2.然后再start(){   

var 一个对象=new 类名();

对象名。属性=值;

对象名。属性=值;

对象名。属性=值;

3.调用JsonUtillity。ToJson(对象名),返回一个string类型的json。

string json=JsonUtillity。ToJson(对象名);

}

​4.2json---转为c#(反序列化)

1.先按着这个json 的内容 先建立一个类 属性就是类型的

获取这个json文件,使用resource.load<textAsset>("路径");返回的是TextAsset类型

获得这个文件里面的内容,变量名。text 返回额是是string类型s

使用JsonUtillity。fromJson<新建的类名>(s文本),返回的是 新建的类名。

报错,因为这个json最外层是是数组结构

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
​
​
​
[System.Serializable]
public class item
{
    public int ID;
    public string Name;
    public string Descript;
    public int Level;
    public int price;
​
}
[System.Serializable]
public class jsonToUse : MonoBehaviour
{   
    //建立json的类
    //1.获得json文件
    //2.获得文件的文字
    //3.解析json
    //4.校验是否正确 输出
​
    void Start()
    {
        TextAsset asset=Resources.Load<TextAsset>("Json/item");
        string json = asset.text;
        List<item> item= JsonUtility.FromJson<List<item>>(json);
        //使用数组来接受,但是不可以
        Debug.Log(item[0].Name);
​
​
    }
​
    
}

json外层结构是数组的解决方法

1.外面再套一层
2.c#的结构变化为,多添加一个类,但是类的一个属性名必须为Data(你添加的属性),属性的类型必须是list<原来的类名>
类都需要加特性
3.调用的时候要多加一层

1.json文件外层包裹一个{"Data":[数组内容]}

{    "Data"  :   [数组内容]
}


2.c#结构变化(类上加特性)



[System.Serializable]
public class Item{
    public List<ItemRow> Data;
}
[System.Serializable]
public class ItemRow{
    public int ID;
    public string Name;
    public string Description;
    public int Level;
    public int Price;
}


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
​
​
//特性
[System.Serializable]
public class ItemJson
{
    public List<item> Data;
}
​
//特性
[System.Serializable]
public class item
{
    public int ID;
    public string Name;
    public string Descript;
    public int Level;
    public int price;
​
}
[System.Serializable]//不知道要不要加?????
public class jsonToUse : MonoBehaviour
{   
    //建立json的类
    //1.获得json文件
    //2.获得文件的文字
    //3.解析json
    //4.校验是否正确 输出
​
    void Start()
    {
        TextAsset asset=Resources.Load<TextAsset>("Json/item");
        string json = asset.text;
​
        ItemJson d = JsonUtility.FromJson<ItemJson>(json);
        Debug.Log(d.Data[2].Name);
​
​
    }
​
    
}

3.使用方法



using System.Collections;
using System.Collections.Generic;
using UnityEngine;
​
​
//特性
[System.Serializable]
public class ItemJson
{
    public List<item> Data;
}
​
//特性
[System.Serializable]
public class item
{
    public int ID;
    public string Name;
    public string Descript;
    public int Level;
    public int price;
​
}
[System.Serializable]//不知道要不要加?????
public class jsonToUse : MonoBehaviour
{   
    //建立json的类
    //1.获得json文件
    //2.获得文件的文字
    //3.解析json
    //4.校验是否正确 输出
​
    void Start()
    {
        TextAsset asset=Resources.Load<TextAsset>("Json/item");
        string json = asset.text;
​
        ItemJson d = JsonUtility.FromJson<ItemJson>(json);
        Debug.Log(d.Data[2].Name);
​
​
    }
​
    
}

成功!!!!!

但是不知道哪里需要使用特性

[System.Serializable]---是转换的json类,但是脚本需要添加吗?

litJson的引入

1.下载

2.删除其他内容,只留下c#文件

 

3.删除一句特性

报错---

jsonWriter文件

4.引入命名空间

using LitJson;//1.引入命名空间

litjson的使用

缺点:速度慢,但是编写比较好,json文件的外层结构是数组也是可以

例子

1.引入litjson
2.引入命名空间
3.使用类名JasonMapper
4.使用方法ToObejct<泛型>
5.校验是否正确
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;//引入命名空间
​
public class ItemRow
{
    public int ID;
    public string Name;
    public string Descript;
    public int Level;
    public int price;
}
​
​
public class litList : MonoBehaviour
{
    void Start()
    {   //获得resource文件夹下的json文件
        TextAsset asset = Resources.Load<TextAsset>("Json/LitJson/lit_list");
​
        //获得json文件的文本
        string json=asset.text;
​
        //使用litjson的jasonmapper类中的toObject方法,读出对应的类型(传入json文件)
        List<ItemRow> d = JsonMapper.ToObject<List<ItemRow>>(json);
​
        //打印出其中的数组的属性
        Debug.Log(d[2].Descript);
        //迷惑一分钟(暂停)
​
​
​
    }
​
​
}
//1.需要对应的c#类结构
//2.获得json文件
//3.获得json的文本;
//4.解析json
//5.打印json是否正确

成功!!!

litjson的使用-案例

1.数组类型

数组类型(int)

List<int>();

[11,5,69,22,68]

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
​
public class SListInt : MonoBehaviour
{   
​
​
​
​
    // Start is called before the first frame update
    void Start()
    {
        textSListInt();
​
    }
    //int类型
    public void textSListInt()
    {
        TextAsset asset = Resources.Load<TextAsset>("Json/LitJson/SimpleList");     
        string json = asset.text;
        Debug.Log(json);
        List<int> d = JsonMapper.ToObject<List<int>>(json);
        Debug.Log(d[3]);
        //22
​
    }
​
​
}

数组类型(str)

["JACK","MARY","PETER","LOUIS","MARYNA","POLE"]

   
 public void textSListStr()
    {
        TextAsset asset = Resources.Load<TextAsset>("Json/LitJson/SimpleListStr");
        string json = asset.text;
        Debug.Log(json);
        List<string> d = JsonMapper.ToObject<List<string>>(json);
        Debug.Log(d[3]);
        //LOUIS
​
    }

2.对象类型

1.引入命名空间LITJSON

2.建立一个类,且属性名和json对应

3.解析json为类对象

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
​
public class Player
{
    public string Name;
    public int Age;
    public string Sex;
}
​
public class SObj : MonoBehaviour
{
​
    // Start is called before the first frame update
    void Start()
    {
        TextAsset asset = Resources.Load<TextAsset>("Json/LitJson/SimpleObj");
        string json = asset.text;
        Player p = JsonMapper.ToObject<Player>(json);
        Debug.Log(p.Name);
        //JACK
    }
​
    
}

3.字典类型

Distionary<string,int>

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
​
public class Dic : MonoBehaviour
{   
    
    // Start is called before the first frame update
    void Start()
    {
        TextAsset assest = Resources.Load<TextAsset>("Json/LitJson/Dic");
        string json = assest.text;//报错 bug路径文件名写错了
        Dictionary<string, string> dic = JsonMapper.ToObject<Dictionary<string, string>>(json);
        Debug.Log(dic["Grade"]);
        //三年级
​
​
​
    }
        
}

注意:

Dictionary<string, string>
​
dic["Grade"]
字典的访问是键的类型是string

4.数组套对象类型

public class ItemRow
{
    public int ID;
    public string Name;
    public string Descript;
    public int Level;
    public int price;
}
//对象对应的映射关系
​
//数组类型泛型是对象
 List<ItemRow> d = JsonMapper.ToObject<List<ItemRow>>(json);
​
​
//对象套数组(数组套对象)
[{
    "ID": 1,
    "Name": "大刀",
    "Descript": "可以涮敌人两个Hp",
    "Level": 3,
    "price": 3
},
{
    "ID": 2,
    "Name": "皇荒杖",
    "Descript": "可以积分,和打怪",
    "Level": 2,
    "price": 2
},
{
    "ID": 3,
    "Name": "雨霖铃",
    "Descript": "迷惑一分钟(暂停)",
    "Level": 3,
    "price": 2
},
{
    "ID": 4,
    "Name": "补气丹",
    "Descript": "可以补充1个HP",
    "Level": 1,
    "price": 1
}
]

案例

[{
    "ID": 1,
    "Name": "大刀",
    "Descript": "可以涮敌人两个Hp",
    "Level": 3,
    "price": 3
},
{
    "ID": 2,
    "Name": "皇荒杖",
    "Descript": "可以积分,和打怪",
    "Level": 2,
    "price": 2
},
{
    "ID": 3,
    "Name": "雨霖铃",
    "Descript": "迷惑一分钟(暂停)",
    "Level": 3,
    "price": 2
},
{
    "ID": 4,
    "Name": "补气丹",
    "Descript": "可以补充1个HP",
    "Level": 1,
    "price": 1
}
]

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;//1.引入命名空间
​
//1.需要对应的c#类结构
//2.获得json文件
//3.获得json的文本;
//4.解析json
//5.打印json是否正确
public class ItemRow
{
    public int ID;
    public string Name;
    public string Descript;
    public int Level;
    public int price;
}
​
​
public class litList : MonoBehaviour
{
   
    void Start()
    {   //获得resource文件夹下的json文件
        TextAsset asset = Resources.Load<TextAsset>("Json/LitJson/lit_list");
​
        //获得json文件的文本
        string json=asset.text;
​
        //使用litjson的jasonmapper类中的toObject方法,读出对应的类型(传入json文件)
        List<ItemRow> d = JsonMapper.ToObject<List<ItemRow>>(json);
​
        //打印出其中的数组的属性
        Debug.Log(d[2].Descript);
​
​
​
    }
​
   
}
​

5.数组套字典

1.建立json文件
2.获取资源
3.访问字典的内容

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
​
public class ListDic : MonoBehaviour
{   
   
    void Start()
    {
        TextAsset assest = Resources.Load<TextAsset>("Json/litJson/ListDic");
        string json = assest.text;
        List<Dictionary<string, string>> litdic = JsonMapper.ToObject<List<Dictionary<string, string>>>(json);
        Debug.Log(litdic[2]["Grade"]);
        //七年级
    }
​
​
}

6.对象套数组(数组套对象)

对象套数组
    1.建立对象
    2.建立对象的属性是list《属性类类型》
    3.建立这个里面的属性的类
​

​
{"Data":
[{
    "ID": 1,
    "Name": "大刀",
    "Descript": "可以涮敌人两个Hp",
    "Level": 3,
    "price": 3
},
{
    "ID": 2,
    "Name": "皇荒杖",
    "Descript": "可以积分,和打怪",
    "Level": 2,
    "price": 2
},
{
    "ID": 3,
    "Name": "雨霖铃",
    "Descript": "迷惑一分钟(暂停)",
    "Level": 3,
    "price": 2
},
{
    "ID": 4,
    "Name": "补气丹",
    "Descript": "可以补充1个HP",
    "Level": 1,
    "price": 1
}
]}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
public class PlayData
{
    public List<PLayerInfo> Data;
​
}
public class PLayerInfo
{
    public int ID;
    public string Name;
    public string Descript;
    public int Level;
    public int price;
}
​
public class ObjList : MonoBehaviour
{  
    void Start()
    {
        TextAsset asset = Resources.Load<TextAsset>("Json/LitJson/ObjList");
        string json = asset.text;
        Debug.Log("拿到json的文本内容");
​
        PlayData pd = JsonMapper.ToObject<PlayData>(json);//报错 因为json文件没有内容
        Debug.Log(pd.Data[2].Name);
        //雨霖铃
       
        
        
        //可以积分,和打怪
    }
​
​
}
//对象套数组
//1.建立对象
//2.建立对象的属性是list《属性类类型》
//3.建立这个里面的属性的类

猜你喜欢

转载自blog.csdn.net/weixin_70271498/article/details/128070348