1 解析字典
1)解析为 JObject
private void ParseJson() { // 解析为JObject
string jsonStr = "{'name': 'zhangsan', 'sex': 'male', 'age': 23}";
JObject jo = JsonConvert.DeserializeObject<JObject>(jsonStr);
print("name=" + jo["name"] + ", sex=" + jo["sex"] + ", age=" + jo["age"]);
}
说明:需要使用以下命名空间:
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
2)解析为 Dictionary
private void ParseJson() { // 解析为Dictionary<string, object>
string jsonStr = "{'name': 'zhangsan', 'sex': 'male', 'age': 23}";
Dictionary<string, object> dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonStr);
print("name=" + dict["name"] + ", sex=" + dict["sex"] + ", age=" + dict["age"]);
}
3)解析为封装类
class Student {
public string name;
public string sex;
public int age;
public override string ToString() {
return "{name: " + name + ", sex: " + sex + ", age: " + age + "}";
}
}
private void ParseJson() { // 解析为Student
string jsonStr = "{'name': 'zhangsan', 'sex': 'male', 'age': 23}";
Student stu = JsonConvert.DeserializeObject<Student>(jsonStr);
print("stu=" + stu);
}
2 解析一维数组
1)解析为 JArray
private void ParseJson() { // 解析一维数组(JArray)
string jsonStr = "[1, 2, 3, 4, 5]";
JArray ja = JsonConvert.DeserializeObject<JArray>(jsonStr);
print(GetJArrayString(ja));
}
private string GetJArrayString(JArray ja) {
string str = "[";
if (ja != null && ja.Count > 0) {
str += ja[0];
}
for (int i = 1; i < ja.Count; i++) {
str += ", " + ja[i];
}
return str + "]";
}
2)解析为数组
private void ParseJson() { // 解析一维数组(int[])
string jsonStr = "[1, 2, 3, 4, 5]";
int[] arr = JsonConvert.DeserializeObject<int[]>(jsonStr);
print(GetArrString(arr));
}
private string GetArrString(int[] arr) {
string str = "[";
if (arr != null && arr.Length > 0) {
str += arr[0];
}
for (int i = 1; i < arr.Length; i++) {
str += ", " + arr[i];
}
return str + "]";
}
3)解析为列表
private void ParseJson() { // 解析一维数组(List<int>)
string jsonStr = "[1, 2, 3, 4, 5]";
List<int> list = JsonConvert.DeserializeObject<List<int>>(jsonStr);
print(GetListString(list));
}
private string GetListString(List<int> list) {
string str = "[";
if (list != null && list.Count > 0) {
str += list[0];
}
for (int i = 1; i < list.Count; i++) {
str += ", " + list[i];
}
return str + "]";
}
3 解析二维数组
1)解析为 JArray
private void ParseJson() { // 解析二维数组(JArray)
string jsonStr = "[[1, 2], [3, 4], [5, 6]]";
JArray ja2 = JsonConvert.DeserializeObject<JArray>(jsonStr);
print(GetJArrayString2(ja2));
}
private string GetJArrayString2(JArray ja2) {
string str = "[";
if (ja2 != null && ja2.Count > 0) {
str += GetJArrayString((JArray)ja2[0]);
}
for (int i = 1; i < ja2.Count; i++) {
str += ", " + GetJArrayString((JArray)ja2[i]);
}
return str + "]";
}
说明:GetJArrayString 方法见 2 1)节。
2)解析为二维数组
private void ParseJson() { // 解析二维数组(int[][])
string jsonStr = "[[1, 2], [3, 4], [5, 6]]";
int[][] arr = JsonConvert.DeserializeObject<int[][]>(jsonStr);
print(GetArrString2(arr));
}
private string GetArrString2(int[][] arr) {
string str = "[";
if (arr != null && arr.Length > 0) {
str += GetArrString(arr[0]);
}
for (int i = 1; i < arr.Length; i++) {
str += ", " + GetArrString(arr[i]);
}
return str + "]";
}
说明:GetArrString 方法见 2 2)节。
4 解析数组字典
1)解析为 JObject-JArray
private void ParseJson() { // "数组字典"混合解析(JObject-JArray)
string jsonStr = "{'vec1': [1, 2, 3, 4], 'vec2': [[1, 2], [3, 4], [5, 6]]}";
JObject jo = JsonConvert.DeserializeObject<JObject>(jsonStr);
print("vec1=" + GetJArrayString((JArray) jo["vec1"]) + ", vec2=" + GetJArrayString2((JArray) jo["vec2"]));
}
说明: GetJArrayString 方法见 2 1)节,GetJArrayString2 方法见 3 1)节。
2)解析为封装类
class VecInfo {
public int[] vec1;
public int[][] vec2;
}
private void ParseJson() { // "数组字典"混合解析(封装类)
string jsonStr = "{'vec1': [1, 2, 3, 4], 'vec2': [[1, 2], [3, 4], [5, 6]]}";
VecInfo vecInfo = JsonConvert.DeserializeObject<VecInfo>(jsonStr);
print("vec1=" + GetArrString(vecInfo.vec1) + ", vec2=" + GetArrString2(vecInfo.vec2));
}
说明: GetArrString 方法见 2 2)节,GetArrString2 方法见 3 2)节。
5 解析字典数组
1)解析为 JArray-JObject
private void ParseJson() { // "字典数组"混合解析(JArray-JObject)
string jsonStr = "[{'name': 'xiaomin', 'sex': 'male', 'age': 23}, {'name': 'xiaohong', 'sex': 'female', 'age': 21}]";
JArray ja = JsonConvert.DeserializeObject<JArray>(jsonStr);
string str = "[";
if (ja.Count > 0) {
str += "{name: " + ja[0]["name"] + ", sex: " + ja[0]["sex"] + ", age: " + ja[0]["age"] + "}";
}
for (int i = 1; i < ja.Count; i++) {
str += ", {name: " + ja[i]["name"] + ", sex: " + ja[i]["sex"] + ", age: " + ja[i]["age"] + "}";
}
str += "]";
print(str);
}
2)解析为数组封装类
private void ParseJson() { // "字典数组"混合解析(Student[])
string jsonStr = "[{'name': 'xiaomin', 'sex': 'male', 'age': 23}, {'name': 'xiaohong', 'sex': 'female', 'age': 21}]";
Student[] stus = JsonConvert.DeserializeObject<Student[]>(jsonStr);
print(GetStudentArrString(stus));
}
private string GetStudentArrString(Student[] stus) {
string str = "[";
if (stus != null && stus.Length > 0) {
str += stus[0];
}
for (int i = 1; i < stus.Length; i++) {
str += ", " + stus[i];
}
return str + "]";
}
说明:Student 类见 1 3)节。
3)解析为列表封装类
private void ParseJson() { // "字典数组"混合解析(List<Student>)
string jsonStr = "[{'name': 'xiaomin', 'sex': 'male', 'age': 23}, {'name': 'xiaohong', 'sex': 'female', 'age': 21}]";
List<Student> list = JsonConvert.DeserializeObject<List<Student>>(jsonStr);
print(GetStudentListString(list));
}
private string GetStudentListString(List<Student> list) {
string str = "[";
if (list != null && list.Count > 0) {
str += list[0];
}
for (int i = 1; i < list.Count; i++) {
str += ", " + list[i];
}
return str + "]";
}
6 解析 Vector3 点集
1)字典格式点
private void ParseJson() {
string jsonStr = "[{'x': 1, 'y': 2, 'z': 3}, {'x': 4, 'y': 5, 'z': 6}, {'x': 7, 'y': 8, 'z': 9}]";
Vector3[] points = JsonConvert.DeserializeObject<Vector3[]>(jsonStr);
print(GetVectorString(points));
}
private string GetVectorString(Vector3[] points) {
string str = "[";
if (points != null && points.Length > 0) {
str += points[0];
}
for (int i = 1; i < points.Length; i++) {
str += ", " + points[i];
}
return str + "]";
}
2)数组格式点
private void ParseJson() {
string jsonStr = "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]";
float[][] arr = JsonConvert.DeserializeObject<float[][]>(jsonStr);
Vector3[] points = new Vector3[arr.Length];
for(int i = 0; i < arr.Length; i++) {
points[i] = new Vector3(arr[i][0], arr[i][1], arr[i][2]);
}
print(GetVectorString(points));
}
说明:GetVectorString 方法同第 1)节。
7 从文件中解析
private void ParseJson() { // 解析为JObject
string jsonStr = File.ReadAllText("Assets/Scenes/Student.json", Encoding.UTF8);
JObject jo = JsonConvert.DeserializeObject<JObject>(jsonStr);
print("name=" + jo["name"] + ", sex=" + jo["sex"] + ", age=" + jo["age"]);
}
说明:File 和 Encoding.UTF8 需要引入命名空间 System.IO、System.Text。
Student.json
{'name': 'zhangsan', 'sex': 'male', 'age': 23}
声明:本文转自【C#】基于JsonConvert解析Json数据
网友评论