1

I have a JSON which I get from web application. I try this parse to Object using System.Web.Script.Serialization . But I have problem with parse

const String jsonWeb = @"{
    ""error"" : 0,
    ""result"" : {
        ""data"" : {
            ""1722278"" : {
                ""din"" : ""741338;361490;648450;934096;180467;209928;805500;283840;901917;110193;9438;363965;700670;647965;18399;31993;714755;242839;60436;600310;"",
                ""pid"" : ""11""
            },
            ""1752279"" : {
                ""din"" : ""970510;771034;372305;286336;926373;459655;1409;140138;692017;311733;686476;368178;735954;635501;816346;821514;768444;510380;959990;916515;"",
                ""pid"" : ""11""
            },
            ""1752280"" : {
                ""din"" : ""992662;474351;139725;998728;475954;706331;798728;744204;999194;22546;177003;4098;448658;842154;431042;970297;989184;362126;7720;720781;"",
                ""pid"" : ""11""
            }
        },
        ""pid"" : {
            ""11"" : {
                ""codejs"" : ""  very \""long\"" String      "",
                ""max"" : ""3""
            }
        },
        ""sys"" : {
            ""11"" : {
                ""1"" : {
                    ""b"" : ""Firefox"",
                    ""s"" : ""Windows""
                },
                ""2"" : {
                    ""b"" : ""Chrome"",
                    ""s"" : ""Windows""
                }
            }
        },
        ""maxgetlimit"" : 10001,
        ""setProxy"" : [],
        ""cidget"" : {
            ""111102"" : 1
        },
        ""openBrowser"" : 1
    }}";

I try parse this using this code but i get error. I try use this:

public class Pack
{
    public string din { get; set; }
    public string pid { get; set; }
}

public class MySybObject
{
    public Pack attributes { get; set; }
}

public class Result
{
    public Result() { data = new List<MySybObject>(); }
    public List<MySybObject> data { get; set; }
}

public class Code
{
    public String codejs { get; set; }
    public String max { get; set; }
}

public class MySubPid
{
    public Code code { get; set; }
}

public class SystemReq
{
    public String b { get; set; }
    public String s { get; set; }
}

public class Sys 
{
    public SystemReq sysreq { get; set; }
}

public class Cidget
{
    public String cidget { get; set; }
}

public class Response
{
    public Response() {}
    public string error { get; set; }
    public Result results { get; set; }
    public MySubPid pid { get; set; }
    public Sys sys { get; set; }
    public Int32 maxgetlimit { get; set; }
    public String setProxy { get; set; }
    public Cidget cidget { get; set; }
    public String openBrowser { get; set; }
}

JavaScriptSerializer ser = new JavaScriptSerializer();
Response foo = ser.Deserialize<Response>(jsonWeb);

But I get Error and I probably lost a number of object in data - this is important data for me.

1
  • I get only null object. Object Response, results is null etc. Commented Mar 23, 2012 at 10:16

2 Answers 2

2

Since there are numbers used as keys in your json string, it would be better to use dynamic objects in this case instead of deserializing to a class (I used Json.Net for this)

dynamic jsonObj = JsonConvert.DeserializeObject(jsonWeb);
Console.WriteLine(jsonObj.result.data["1722278"].pid);
Console.WriteLine(jsonObj.result.sys["11"]["1"].b);

BTW: this line is wrong

""codejs"" : ""  very ""long"" String      ""

It should be something like this

""codejs"" : ""  very \""long\"" String      "",

--EDIT--

foreach (var child in jsonObj.result.data.Children())
{
    Console.WriteLine(child.Name + "=> " + child.Value.din);
}
Sign up to request clarification or add additional context in comments.

1 Comment

wow great but I have a one question. Console.WriteLine(jsonObj.result.data["1722278"].pid); but I don't know number of data - this number are uses as key but value is random. Can I get information which key is available ?
0

Have a try of JSON class generator to generate your classes, then try it again.

Update: I had to change the JSON string in order to generate it;

{
    "error" : 0,
    "result" : {
        "data" : {
            "1722278" : {
                "din" : "741338;361490;648450;934096;180467;209928;805500;283840;901917;110193;9438;363965;700670;647965;18399;31993;714755;242839;60436;600310;",
                "pid" : "11"
            },
            "1752279" : {
                "din" : "970510;771034;372305;286336;926373;459655;1409;140138;692017;311733;686476;368178;735954;635501;816346;821514;768444;510380;959990;916515;",
                "pid" : "11"
            },
            "1752280" : {
                "din" : "992662;474351;139725;998728;475954;706331;798728;744204;999194;22546;177003;4098;448658;842154;431042;970297;989184;362126;7720;720781;",
                "pid" : "11"
            }
        },
        "pid" : {
            "11" : {
                "codejs" : "very long String      ",
                "max" : "3"
            }
        },
        "sys" : {
            "11" : {
                "1" : {
                    "b" : "Firefox",
                    "s" : "Windows"
                },
                "2" : {
                    "b" : "Chrome",
                    "s" : "Windows"
                }
            }
        },
        "maxgetlimit" : 10001,
        "setProxy" : [],
        "cidget" : {
            "111102" : 1
        },
        "openBrowser" : 1
    }}

1 Comment

But this generate 1722278 from data as class. This is bad for me.

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.