Tom
Tom

Reputation: 129

C# deserialization of JSON

I have this JSON string:

{"natalia1891":{"idUser":1435105,"nick":"natalia1891","sefNick":"natalia1891","status":1,"photo":"http:\/\/213.215.107.125\/fotky\/143\/51\/s_1435105.jpg?v=3","sex":2,"isFriend":1},

"pepina888":{"idUser":3338870,"nick":"pepina888","sefNick":"pepina888","status":1,"photo":"http:\/\/213.215.107.127\/fotky\/333\/88\/s_3338870.jpg?v=9","sex":2,"isFriend":1}}

I would like to deserialize this JSON into a dictionary of object Friend. Any advice?

maybe:

class Friend{

public string Name{get;set;}

public string IdUser{get;set;}

public string SefNick{get;set;}

public bool Status{get;set;}

public string Url{get;set;}

public int Sex{get;set;}

public bool isFriend{get;set;}
}

Upvotes: 3

Views: 2469

Answers (4)

Olle
Olle

Reputation: 29

Friend[] friends = new JavaScriptSerializer().Deserialize<Friend[]>(myString);

JavaScriptSerializer is in System.Web.Script.Serialization.

Upvotes: 0

SLaks
SLaks

Reputation: 887195

You can use the JsonDataContractSerializer class in the .Net framework.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499770

Have a look at the Json.NET documentation on serializing/deserializing.

You may need to do a little work to effectively make it case-insensitive, although it looks like James made it fairly forgiving a while ago.

Upvotes: 4

James Sumners
James Sumners

Reputation: 14775

Use JSON.Net.

Upvotes: 4

Related Questions