2

Hey guys i want to parse this type of string:

    {"1":
    [
        {"OrderItemId":832,"ItemId":995,"Price":0},
        {"OrderItemId":833,"ItemId":997,"Price":0},
        {"OrderItemId":834,"ItemId":999,"Price":0}
    ],
    "2":
    [
        {"OrderItemId":835,"ItemId":1008,"Price":0},
        {"OrderItemId":836,"ItemId":1057,"Price":0}
    ]
}

its very important for me, so please guys help me.

0

6 Answers 6

4

Like this, if you want to access the OrderItemId

var test =  {"1":
    [
        {"OrderItemId":832,"ItemId":995,"Price":0},
        {"OrderItemId":833,"ItemId":997,"Price":0},
        {"OrderItemId":834,"ItemId":999,"Price":0}
    ],
    "2":
    [
        {"OrderItemId":835,"ItemId":1008,"Price":0},
        {"OrderItemId":836,"ItemId":1057,"Price":0}
    ]
}

alert(test["1"][0]['OrderItemId']);
alert(test["1"][1]['OrderItemId']);
alert(test["1"][2]['OrderItemId']);

alert(test["2"][0]['OrderItemId']);
alert(test["2"][1]['OrderItemId']);

UPDATE, to iterate trough test with unknown lengths

for (var i=0;i<test["1"].length;i++) {
    alert(test["1"][i]['OrderItemId']);
}

Here with full automatic enumaration

var number, count = 0;
var en = [];
for(number in test) {
    en[count]=number;
    count++;
}
for (var r=0;r<en.length;r++) {
    alert(en[r]);
    for (var i=0;i<test[en[r]].length;i++) {
        alert(test[en[r]][i]['OrderItemId']);
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

cool. but how can i do it when i don't know the the length? i mean to say that, how can i do it dynamically..?
still i have to give test["1"] as static. i don't want to give "1" as static. i want to parse it for any number of array..
Hey i am not fully satisfied.Please give me solution so that i can mark your answer as accepted.
have made yet another update, last one - it should be clear now :-)
got the exact answer what i want. thanx a ton. :)
3

This is JSON. Use this to parse it. (or any other library you preffer, maybe jQuery)

1 Comment

+1 for concisitude. You might want to give a JSON.parse code sample.
2

//You can do by this way also :

<script>
    var employees = { "accounting" : [   // accounting is an array in employees.
                                    { "firstName" : "John",  // First element
                                      "lastName"  : "Doe",
                                      "age"       : 23 },

                                    { "firstName" : "Mary",  // Second Element
                                      "lastName"  : "Smith",
                                      "age"       : 32 }
                                  ], // End "accounting" array.                                  
                  "sales"       : [ // Sales is another array in employees.
                                    { "firstName" : "Sally", // First Element
                                      "lastName"  : "Green",
                                      "age"       : 27 },

                                    { "firstName" : "Jim",   // Second Element
                                      "lastName"  : "Galley",
                                      "age"       : 41 }
                                  ] // End "sales" Array.
                } // End Employees


    var output="<ul>";
    for (var i in employees.accounting) {
        output+="<li>" + employees.accounting[i].firstName + " " + employees.accounting[i].lastName + "--" + employees.accounting[i].age+"</li>";
    }
    output+="</ul>";

    var output1="<ul>";
    for (var j in employees.sales) {
        output1+="<li>" + employees.sales[j].firstName + " " + employees.sales[j].lastName + "--" + employees.sales[j].age+"</li>";
    }
    output+="</ul>";

    alert(output1);

</script>

2 Comments

i want that parsing takes "sales" or "accounting" itself. any idea..?
coz its possible that may be hundreds of names like "sales" and "accounting". what to do..?
0

The problem might have been due to line ending.

Here is a working[tested in chrome dev console] code:

var jsonStr = '{"1":\
    [\
        {"OrderItemId":832,"ItemId":995,"Price":0},\
        {"OrderItemId":833,"ItemId":997,"Price":0},\
        {"OrderItemId":834,"ItemId":999,"Price":0}\
    ],\
    "2":\
    [\
        {"OrderItemId":835,"ItemId":1008,"Price":0},\
        {"OrderItemId":836,"ItemId":1057,"Price":0}\
    ]\
}';
var obj = JSON.parse(jsonStr); // works!

1 Comment

i have applied "\" for line ending.
0

You can use jQuery to Parse JSON

Also have a look at this SO question Parse JSON in JavaScript?

Comments

0

There is no need to add backward slashes after ending if every object item. You can separate with comma only.

Comments

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.