0

Right now my json looks easy and there is one object. suppose there would be many object in json then how could i parse it.

Now my json looks like

    var jsonText = @"{
        ""some_number"": 108.541, 
        ""date_time"": ""2011-04-13T15:34:09Z"", 
        ""serial_number"": ""SN1234"",
        ""more_data"": {
            ""field1"": 1.0,
            ""field2"": ""hello""
        }
    }";

i am parsing it this way

    var jss = new JavaScriptSerializer();
    var dict = jss.Deserialize<dynamic>(jsonText);

    Console.WriteLine(dict["some_number"]); 
    Console.WriteLine(dict["more_data"]["field2"]);
    Console.ReadLine();

when there will be nested object in each ordinal of json array then how could i parse with JavaScriptSerializer?

Suppose my json look bit complicated like below then how could i parse it by JavaScriptSerializer.

    var jsonText = @"data[
    {
        ""some_number"": 108.541, 
        ""date_time"": ""2011-04-13T15:34:09Z"", 
        ""serial_number"": ""SN1234"",
        ""more_data"": {
            ""field1"": 1.0,
            ""field2"": ""hello""
        },
        ""Hobbies"" : [""game1"",""game2"",""game3""]
    },
    {
        ""some_number"": 200.541, 
        ""date_time"": ""2012-04-11T15:34:09Z"", 
        ""serial_number"": ""SN3333"",
        ""more_data"": {
            ""field1"": 2.0,
            ""field2"": ""hello1""
        },
        ""Hobbies"" : [""game4"",""game5"",""game6""]
    }]";

    var jss = new JavaScriptSerializer();
    var dict = jss.Deserialize<dynamic>(jsonText);

    Console.WriteLine(dict["some_number"]); 
    Console.WriteLine(dict["more_data"]["field2"]);
    Console.ReadLine();

please tell me how could i parse the above json with JavaScriptSerializer. Thanks

EDIT

Change the json as per your guidance. now tell me does it look right?

    var jsonText = @"{ ""data""[
    {
        ""some_number"": 108.541, 
        ""date_time"": ""2011-04-13T15:34:09Z"", 
        ""serial_number"": ""SN1234"",
        ""more_data"": {
            ""field1"": 1.0,
            ""field2"": ""hello""
        },
        ""Hobbies"" : [""game1"",""game2"",""game3""]
    },
    {
        ""some_number"": 200.541, 
        ""date_time"": ""2012-04-11T15:34:09Z"", 
        ""serial_number"": ""SN3333"",
        ""more_data"": {
            ""field1"": 2.0,
            ""field2"": ""hello1""
        },
        ""Hobbies"" : [""game4"",""game5"",""game6""]
    }]}";

2 Answers 2

1

Your jsonText is not a valid json. You probably need something like:

var jsonText = @"{
                  ""data"":[
                      {......

make your json valid and the JavaScriptSerializer will work.

Edit

If I am guessing right,, your object needs to be

var jsonText = @"{""data"":[
{
    ""some_number"": 108.541, 
    ""date_time"": ""2011-04-13T15:34:09Z"", 
    ""serial_number"": ""SN1234"",
    ""more_data"": {
        ""field1"": 1.0,
        ""field2"": ""hello""
    },
    ""Hobbies"" : [""game1"",""game2"",""game3""]
},
{
    ""some_number"": 200.541, 
    ""date_time"": ""2012-04-11T15:34:09Z"", 
    ""serial_number"": ""SN3333"",
    ""more_data"": {
        ""field1"": 2.0,
        ""field2"": ""hello1""
    },
    ""Hobbies"" : [""game4"",""game5"",""game6""]
}]}";

so now you can access your object like...

dict["data"]  //which is your array containing your two objects
dict["data"][0]["some_number"] //eg first object property
dict["data"][1]["more_data"]["field2"] //second object property...

use your watch panel to debug your object.. You should create a model to cast your object into your models. Having unknown models should be avoided unless needed.

Sign up to request clarification or add additional context in comments.

2 Comments

if possible give me bit of code to read some from json array with JavaScriptSerializer. thanks
see my edit. change the json as per your guidance. now tell me does it look right?
0

I sort the problem. @itdoesntwork hint really help me to fix the json error. Here i am sharing my code to show how i did it.

Please add reference of System.Web.Extensions and add this namespace using System.Web.Script.Serialization; at top

    var jsonText = @"{ ""data"":[
    {
        ""some_number"": 108.541, 
        ""date_time"": ""2011-04-13T15:34:09Z"", 
        ""serial_number"": ""SN1234"",
        ""more_data"": {
            ""field1"": 1.0,
            ""field2"": ""hello""
        },
        ""Hobbies"" : [""game1"",""game2"",""game3""]
    },
    {
        ""some_number"": 200.541, 
        ""date_time"": ""2012-04-11T15:34:09Z"", 
        ""serial_number"": ""SN3333"",
        ""more_data"": {
            ""field1"": 2.0,
            ""field2"": ""hello1""
        },
        ""Hobbies"" : [""game4"",""game5"",""game6""]
    }]}";

    var jss = new JavaScriptSerializer();
    var dict = jss.Deserialize<dynamic>(jsonText);

    Console.WriteLine(dict["data"][0]["some_number"]);
    Console.WriteLine(dict["data"][0]["more_data"]["field2"]);

    Console.WriteLine(dict["data"][1]["some_number"]);
    Console.WriteLine(dict["data"][1]["more_data"]["field2"]);
    Console.WriteLine(dict["data"][1]["Hobbies"][1]);

    Console.ReadLine();

2 Comments

you outran me :P
but i insist.. use models.. avoid using unknown objects.. if your project goes big, many people work on it, or you see your code after months,, unknown objects will be a mess.

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.