2

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

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer. But the problem is related with this specific json string.
@user285677: So have you tried using Json.NET with that string? If so, what goes wrong?
The problem is I can't desirealized all the json string directly to a c# class because the "comments" is made of one dictionary with an array of messages and a paging class.
3

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.

1 Comment

I'm not sure, but it's almost as if the OP's asking for a class that would serialize to something like the example.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.