0
public class SocialFriends
    {
        public string id { get; set; }
        public string name { get; set; }
    }

this is my Class.

List<SocialFriends> oList2 = ser.Deserialize<List<SocialFriends>>(response.Content);

I'm getting data's like this. But it returns 0 data :S

Data is here

{"data":[{"name":"George","id":"511222445"},{"name":"Mayk","id":"517247768"}]}

I can't explain this problem? Can anybody say, where is my fault?

3 Answers 3

1

Create a class

class Data 
{
    public SocialFriends[] data { get; set; }
}

and change your code to:

Data oList2 = ser.Deserialize<Data>(response.Content);
Sign up to request clarification or add additional context in comments.

1 Comment

I used Your solution, and it worked, thanks. I getted the datas like this. foreach (var friend in oList2.data) Response.Write(friend.id);
1

This code works.

public class SocialFriendsData
{
    public List<SocialFriends> Data { get; set; }
}

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

Deserialization:

 JavaScriptSerializer ser = new JavaScriptSerializer();
 string response = "{\"data\":[{\"name\":\"George\",\"id\":\"511222445\"},{\"name\":\"Mayk\",\"id\":\"517247768\"}]}";
 SocialFriendsData oList2 = ser.Deserialize<SocialFriendsData>(response);

Comments

0

Your JSON is wrapped in a data property. You'll have to grab the JSON string out of that data property. I don't know which JSON serializer you use, but based on what you provided, the easiest way is probably just to create an intermediate DataHolder class:

public class DataHolder
{
    public string Data { get; set; }
}

Then deserialize it like this:

var dataHolder = ser.Deserialize<DataHolder>(response.Content);
var oList2 = ser.Deserialize<List<SocialFriends>>(dataHolder.Data);

If you're using a robust JSON serializer like Json.NET, you can even skip the intermediate deserialization and change your DataHolder type to the correct type:

public class DataHolder
{
    public List<SocialFriends> SocialFriends { get; set; }
}

And then use this code to get the data:

var dataHolder = ser.Deserialize<DataHolder>(response.Content);
var oList2 = dataHolder.SocialFriends;

1 Comment

That would completely depend on your JSON serializer. I know Json.NET doesn't care.

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.