0
{"id":"190155258007","name":"name","email":"mail@gmail.com","gender":"male",
"friends":{"data":[{"id":"1331173146","name":"friendname1"},{"id":"120497111959","name":"friendname2"},{"id":"9980211103","name":"friendname3"},
{"id":"77872075894","name":"friendname4"}]}

I am getting this JSON result but I can get deserialized value from single values but this friends array returns a null value in C#. How to fix this?

Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
selvam
  • 15
  • 5

1 Answers1

0

Use Newtonsoft.Json

Create your custom class:

public class MyResponse {
    public string id { get; set; }
    public string name { get; set; }
    public string email { get; set; }
    public string gender { get; set; }
    public friend friends { get; set; }

    public class friend{
        public List<data> data {get;set;}

        public class data {
            string id {get;set;}
            string name {get;set;}
        }
    }
}

Now you can deserialize your json like:

MyResponse response = Newtonsoft.Json.JsonConvert.DeserializeObject<MyResponse>("You json string here");

Now this response object will be filled by your return json string. If you don't have Newtonsoft.Json dll, you can download it from the following link:

https://www.nuget.org/packages/newtonsoft.json/

if you dont want to use custom class, you can use dynamic keyword like:

dynamic stuff = JsonConvert.DeserializeObject("Your json string here");

string name = stuff.name;
string email= stuff.email;

And so on.

Muhammad Qasim
  • 1,622
  • 14
  • 26
  • Thanks for your Reply.. But getting error like 'Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[App.friend]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. ' – selvam Feb 22 '17 at 11:27
  • @selvam: Have you created the custom class exactly i have mentioned in my answer? – Muhammad Qasim Feb 22 '17 at 11:29