-1

Possible Duplicate:
I have a nested data structure / JSON, how can access a specific value?

I have a JSON data that is in the below format. I need to get the store name corresponding to the one which has specialDeal as true in "Non Veg". (NOTE: Only one of the stores will have specialDeal as true) How will i retrieve it. Please help. thanks in advance .. Please help me as i am just learning Java.

 {"Food":
  {
   "Veg":
    {
        "amtSpent":"", "shortDesc":"", "longDesc":"", "excTxt":"", 
        "discntType":"", "Store":"", "StoreType":"", "Fund":"",
        "FundDetails":[
                {"status":"", "discntVal":"", "FundVal":"", "FundBal":""},
                {"status":"", "discntVal":"", "FundVal":"", "FundBal":""}
        ]
     },
   "Non Veg":
        {
        "chicken":
                [
                {
                        "amtSpent":"", "shortDesc":"", "longDesc":"", "excTxt":"",
                        "discntType":"", "Store":"", "StoreType":"", "Fund":"",
                        "specialDeal":"", "promoStatus":"",
                        "FundDetails":[
                                {"status":"", "discntVal":"", "FundVal":"", "FundBal":""},
                                {"status":"", "discntVal":"", "FundVal":"", "FundBal":""}
                        ]
     },
     {
                        "amtSpent":"", "shortDesc":"", "longDesc":"", "excTxt":"",
                        "discntType":"", "Store":"", "StoreType":"", "Fund":"",
                        "specialDeal":"", "promoStatus":"",
                        "FundDetails":[
                                {"status":"", "discntVal":"", "FundVal":"", "FundBal":""},
                                {"status":"", "discntVal":"", "FundVal":"", "FundBal":""}
                        ]
     }
    ],
        "fish":
                [
                {
                        "amtSpent":"", "shortDesc":"", "longDesc":"", "excTxt":"",
                        "discntType":"", "Store":"", "StoreType":"", "Fund":"",
                        "specialDeal":"", "promoStatus":"",
                        "FundDetails":[
                                {"status":"", "discntVal":"", "FundVal":"", "FundBal":""},
                                {"status":"", "discntVal":"", "FundVal":"", "FundBal":""}
                        ]
                },
                {
                        "amtSpent":"", "shortDesc":"", "longDesc":"", "excTxt":"",
                        "discntType":"", "Store":"", "StoreType":"", "Fund":"",
                        "specialDeal":"", "promoStatus":"",
                        "FundDetails":[
                                {"status":"", "discntVal":"", "FundVal":"", "FundBal":""},
                                {"status":"", "discntVal":"", "FundVal":"", "FundBal":""}
                        ]
                }
                ],
        "egg":
                [
                {
                        "amtSpent":"", "shortDesc":"", "longDesc":"", "excTxt":"",
                        "discntType":"", "Store":"", "StoreType":"", "Fund":"",
                        "specialDeal":"", "promoStatus":"",
                        "discntVal":"", "FundVal":"", "FundBal":""
                },
                {
                        "amtSpent":"", "shortDesc":"", "longDesc":"", "excTxt":"",
                        "discntType":"", "Store":"", "StoreType":"", "Fund":"",
                        "specialDeal":"", "promoStatus":"",
                        "discntVal":"", "FundVal":"", "FundBal":""
                }
                         ]
        },
"isMember":"Y",
"orderId":""

}

Community
  • 1
  • 1
JohnJohny
  • 65
  • 5

2 Answers2

0

OK, so the JSON is missing some curly braces somewhere - I assumed they were at the very end.

Here's the process:

// s_json is the string form of your json response
var s_json='{"Food":  {   "Veg":    {        "amtSpent":"", "shortDesc":"", "longDesc":"", "excTxt":"",         "discntType":"", "Store":"", "StoreType":"", "Fund":"",        "FundDetails":[                {"status":"", "discntVal":"", "FundVal":"", "FundBal":""},                {"status":"", "discntVal":"", "FundVal":"", "FundBal":""}        ]     },   "Non Veg":        {        "chicken":                [                {                        "amtSpent":"", "shortDesc":"", "longDesc":"", "excTxt":"",                        "discntType":"", "Store":"result", "StoreType":"", "Fund":"",                        "specialDeal":"true", "promoStatus":"",                        "FundDetails":[                                {"status":"", "discntVal":"", "FundVal":"", "FundBal":""},                                {"status":"", "discntVal":"", "FundVal":"", "FundBal":""}                        ]     },     {                        "amtSpent":"", "shortDesc":"", "longDesc":"", "excTxt":"",                        "discntType":"", "Store":"", "StoreType":"", "Fund":"",                        "specialDeal":"", "promoStatus":"",                        "FundDetails":[                                {"status":"", "discntVal":"", "FundVal":"", "FundBal":""},                                {"status":"", "discntVal":"", "FundVal":"", "FundBal":""}                        ]     }    ],        "fish":                [                {                        "amtSpent":"", "shortDesc":"", "longDesc":"", "excTxt":"",                        "discntType":"", "Store":"", "StoreType":"", "Fund":"",                        "specialDeal":"", "promoStatus":"",                        "FundDetails":[                                {"status":"", "discntVal":"", "FundVal":"", "FundBal":""},                                {"status":"", "discntVal":"", "FundVal":"", "FundBal":""}                        ]                },                {                        "amtSpent":"", "shortDesc":"", "longDesc":"", "excTxt":"",                        "discntType":"", "Store":"", "StoreType":"", "Fund":"",                        "specialDeal":"", "promoStatus":"",                        "FundDetails":[                                {"status":"", "discntVal":"", "FundVal":"", "FundBal":""},                                {"status":"", "discntVal":"", "FundVal":"", "FundBal":""}                        ]                }                ],        "egg":                [                {                        "amtSpent":"", "shortDesc":"", "longDesc":"", "excTxt":"",                        "discntType":"", "Store":"", "StoreType":"", "Fund":"",                        "specialDeal":"", "promoStatus":"",                        "discntVal":"", "FundVal":"", "FundBal":""                },                {                        "amtSpent":"", "shortDesc":"", "longDesc":"", "excTxt":"",                        "discntType":"", "Store":"", "StoreType":"", "Fund":"",                        "specialDeal":"", "promoStatus":"",                        "discntVal":"", "FundVal":"", "FundBal":""                }               ]        },"isMember":"Y","orderId":""}}';
// We use a json parser to turn the string into an object, o_json. In firefox, instead of using jQuery you could use JSON.parse(s_json). Other parsers are available in other frameworks/browsers
var o_json=jQuery.parseJSON(s_json);
// Initialize an empty string for the result
var result=""
// look in the o_json object for the food object property, then grab it's object property with the key "Non Veg". loop through each of the enumerable properties of this object, calling each one o_food
for each (o_food in o_json.Food["Non Veg"]) {
    // Loop through all the elements of the array o_food
    for (var i=0;i<o_food.length;i++) {
        // Does this element of the array have a property, specialDeal, whose value is "true"? If so, check the same element for a property called Store, and use it's value for the value of result
        if (o_food[i].specialDeal=="true") result=o_food[i].Store;
    }
}
// Alert the result
alert(result);

you'll notice I changed your json a little so it finds a result. Once you use a framework (like jQuery) or the native json parsers available in some browsers (like ff) you can treat the json as though it were a normal object

Chris O'Kelly
  • 1,863
  • 2
  • 18
  • 35
0

You may try this :

var nonVeg = jsonData.Food['Non Veg']; // jsonData is json data which you have given above
for(var key in nonVeg) {
  for(var i=0; i<nonVeg[key].length; i++) { 
     var storeObj = nonVeg[key][i];
     if(storeObj['specialDeal']== 'true'){ 
        console.log(storeObj['Store']);
     }
  }
}

since we are interested in only Non Veg part of the JSON, we can store it to some variable i,e. var nonVeg = jsonData.Food['Non Veg']; now we can say nonVeg as a object alone. in this object it has few more objects, so lets iterate through each one with for loop, and store it to a variable called storeObj. since storeObj has many key-value parametres lets iterate each storeObj with for loop. and finally lets check whether the specialDeal property has a value of true? if yes then console.log its 'Store' property i,e. storeObj['Store'].

Shreedhar
  • 5,502
  • 3
  • 22
  • 27
  • Thanku shreedhar..can u explain a bit in few line on how it works..? Not getting it as i am a fresher. Thanks again in advance – JohnJohny Nov 23 '12 at 07:35
  • @JohnJohny edited my answer with expalnation. – Shreedhar Nov 23 '12 at 08:28
  • `if(storeObj['specialDeal']== true)` seems a little unnecessary, isn't it just the same as `if (storeObj['specialDeal'])`? If you wanted to only match for boolean true, rather than type coerced true you could do `if(storeObj['specialDeal']=== true)` – Chris O'Kelly Nov 25 '12 at 22:24
  • @ChrisO'Kelly thanks for pointing it out. :) i must be comparing for value "true" instead of boolean. thanks a lot :) – Shreedhar Nov 26 '12 at 12:18