<返回更多

在C#中处理解析Json对象

2022-05-05    王東東Rex
加入收藏

需要处理的JSON文本文件

{

"name":"王東東",

"age":27,

"other":[

{"id":1,"name":"Rex","work":"bilibili"},

{"id":2,"name":"Rex","work":"今日头条"},

{"id":3,"name":"Rex","work":"爱奇艺"}

]

}

程序代码示例

using System;

using System.Collections.Generic;

using System.Text;

namespace _113_JSON

{

public class Author

{

public string name { get; set; }

public int age { get; set; }

public List<Other> other { get; set; }

public override string ToString()

{

return string.Format("NAME:{0},AGE:{1},OTHERLIST:{2}",name,age

,other);

}

}

}

 

using System;

using System.Collections.Generic;

using System.Text;

namespace _113_JSON

{

public class Other

{

public int id { get; set; }

public string name { get; set; }

public string work { get; set; }

public override string ToString()

{

return string.Format("ID:{0},NAME:{1},WORK:{2}",id,name,work);

}

}

}

 

using LitJson;

using System;

using System.IO;

using System.Collections.Generic;

namespace _113_JSON

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine("在C#中处理解析Json对象");

Author author = JsonMApper.ToObject<Author>(File.ReadAllText("王東東Rex.txt"));

Console.WriteLine(author);

foreach (var item in author.other)

{

Console.WriteLine(item);

}

Console.ReadKey();

}

}

}

 

程序执行结果

 

在C#中处理解析Json对象

 

声明:本站部分内容来自互联网,如有版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
▍相关推荐
更多资讯 >>>