W
W
wrewolf2012-02-02 12:08:39
C++ / C#
wrewolf, 2012-02-02 12:08:39

how to deserialize such json in c#

{"response":[64,{"pid":"114272714","aid":"33565378",
"owner_id":"5005272","created":"1214309659","text":"test",
"src":"http:\/\/cs1437.vkontakte.ru\/u5005272\/33565378\/m_2a07b7cb.jpg",
"src_big":"http:\/\/cs1437.vkontakte.ru\/u5005272\/33565378\/x_5769a2b7.jpg",
"src_small":"http:\/\/cs1437.vkontakte.ru\/u5005272\/33565378\/s_beb1d458.jpg"}]}


public class UserPhotos
{
public Int32 pid { get; set; }
public Int32 aid { get; set; }
public Int32 owner_id { get; set; }
public Int32 created { get; set; }
public string text { get; set; }
public string src { get; set; }
public string src_big { get; set; }
public string src_small { get; set; }
}

public class vkUserPhotos
{
public List response { get; set; }
}

сейчас я вырезаю первый элемент, и десеареилизую в такую штуку
System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string oList = oSerializer.Deserialize(json);

Answer the question

In order to leave comments, you need to log in

6 answer(s)
R
retran, 2012-02-02
@retran

Why reinvent the wheel?
msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx

A
Anatoly, 2012-02-02
@taliban

Why serialize a string? Sealing after all means translation of objects in a line.

A
AxisPod, 2012-02-02
@AxisPod

Hmm, is it not fate to google? "json c#" gives good results.

F
Fr3nzy, 2012-02-02
@Fr3nzy

Try james.newtonking.com/projects/json-net.aspx Have
used it a few times already and it seems to be pretty good :)

R
rPman, 2012-02-02
@rPman

Here is a primitive json decoder (even two), carefully coded, but well commented, written in order not to carry heavy 'correct' json projects with me (plus it does not require new versions of .net, It will work almost with 1.1, incl. h. mono), requires additional processing of syntax errors, etc. (read todo: in comments).
pastebin.com/5jJsVs39
First method SimpleJSON::pairsParseJSON(string) - returns a single-level Dictionary<string, string>, each entry is leaf elements in the json tree, keys are a compound name of the elements separated by a dot (arrays are named through '[index] '), For example:

{test:[{a:1,b:'asd'},123]}

will give an array:
"test[0].a" => "1"
"test[0].b" => "asd"
"test[1]" => "123"
ps this method is very handy when dealing with complex json structures without arrays

The second method SimpleJSON::treeParseJSON(string) will return the classic multi-level Dictionary<string, object>, where the object can be either a string, or a number, or another dictionary.

D
DraculaDIS, 2012-02-02
@DraculaDIS

I use this json2csharp.com/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question