1

I'm using JSON.Net to try and deserialize some responses from facebook. Here's a snapshot of the data I'm reading in:

 {
      "data": [
        {
          "id": "123"
        },
        {
          "id": "234"
        }
      ],
      "paging": {
        "cursors": {
          "before": "xxx",
          "after": "xxx"
        },
        "next": "xxx"
      }
 }

classes:

class jsonDeserialize
{
    public List<ListDetail> ListDetail { get; set; }
}

public class DataList
{
    public string id { get; set; }
}

public class Paging
{
    public List<string> cursors { get; set; }
    public string next { get; set; }
}
public class Cursors
{
    public string before { get; set; }
    public string after { get; set; }
}

public class ListDetail
{
    public List<Cursors> Cursors { get; set; }
    public List<Paging> Articles { get; set; }
    public List<DataList> DataList { get; set; }
}

I'm using this code to use the JSON.NET Deserialize function: for some reason, result return null, please help me :(

var result = JsonConvert.DeserializeObject<jsonDeserialize>(jsonString);

3 Answers 3

1

Fix your classes defenition:

    class jsonDeserialize
    {
        public List<DataList> data { get; set; }
        public Paging paging { get; set; }
    }

    public class DataList
    {
        public string id { get; set; }
    }

    public class Paging
    {
        public Cursors cursors { get; set; }
        public string next { get; set; }
    }
    public class Cursors
    {
        public string before { get; set; }
        public string after { get; set; }
    }

jsonDeserialize contains list of DataList, ListDetail can me removed, cursors is not an array, it's an object.

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

Comments

0

Omg, I've missed it is JSON from facebook and "solved" reversed problem. I'll keep it here, though, just in case

First, as quick remark, your deserialization code here must look smth like var result = JsonConvert.DeserializeObject<jsonDeserialize>(jsonString);

Your JSON does not correspond to code. If you want deserialize to jsonDeserialize class your json must look like that (pay close attention to [] and {})

{
  "ListDetail" : 
  [
   {
     "Cursors":
     [
       {
         "before" : "value",
         "after" : "value"
       }

      ],
      "Articles":
      [
        {
          "Cursors":
          [

            "cursor1",
            "cursor2"

          ],
          "next" : "somenext"
        }
      ],
      "DataList":
      [
        {
          "id" : "someid"
        },
        {
          "id" : "someid2"
        }
      ]
   }
  ]
}

Comments

0
 class jsonDeserialize
    {

        public List<DataList> data { get; set; }
        public Paging paging { get; set; }

    }

    public class DataList
    {
        public string id { get; set; }
    }

    public class Paging
    {
        public Cursors cursors { get; set; }
        public string next { get; set; }
    }
    public class Cursors
    {
        public string before { get; set; }
        public string after { get; set; }
    }

var result=JsonConvert.DeserializeObject<jsonDeserialize>(jsonformat.Substring(0,jsonformat.Length));

Use it it will give you write result. beacuse data and paging treated as properties so that you need to intialize those properties in your class jsondeserailized and instead of list use cursors because you storing cursors data in paging class .Thanks

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.