目录1 解析字典1)解析为 JObject2)解析为 Dictionary3)解析为封装类2 解析一维数组1)解析为 jarray2)解析为数组3)解析为列表3 解析二维数组1)解析
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;
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"]);
}
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);
}
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 + "]";
}
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 + "]";
}
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 + "]";
}
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)节。
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)节。
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)节。
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);
}
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)节。
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 + "]";
}
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)节。
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数据的文章就介绍到这了,更多相关C#解析Json数据内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: C#基于JsonConvert解析Json数据的方法实例
本文链接: https://lsjlt.com/news/208517.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0