开发者

C# class to parse JSON result

How should be a c# class to deserialize the folowing JSON string:

{
   "data": [
  {
 "id" : "id0012",
     "comments": {
        "data": [
           {
              "id": "123",
              "from": {
                 "name": "xpto",
                 "id": "5ddd"
              },
              "message": "tttt",
              "created_time": 开发者_开发知识库"2010-01-07T09:16:15+0000"
           },
           {
              "id": "222",
              "from": {
                 "name": "wwwww",
                 "id": "343434"
              },
              "message": "3333",
              "created_time": "2020-07-07T09:30:12+0000"
           }
        ],
        "paging": {
           "previous": "prevlink",
           "next": "nextLink"
        }
     }
  }

] }

Thanks


Well, there are a lot of different JSON libraries for the .NET Framework. See the C# section at json.org for a list of several different implementations. The best well known libraries of those probably is Json.NET which supports serialization, LINQ, BSON and more! For a quick start on reading JSON with Json.NET, you should have a look at Reading and Writing JSON.


As hangy says, you can certainly use Json.NET to parse that string. For example:

using System;
using System.IO;
using Newtonsoft.Json.Linq;

namespace EvalTest
{
  public class Test
  {
    static void Main(string [] args)
    {
        string text = File.ReadAllText("test.txt");
        var json = JObject.Parse(text);

        var data = json["data"];
        Console.WriteLine((string) data[0]["id"]);
    }
  }
}

If you need more help, you should ask a more specific question.


I use this tool Jsonclassgenerator. It could generate C# classes for the input Json string. You dont have to use the exact class generated. But you can see the format of the class and create a similar one in your project.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜