简体   繁体   中英

Is it possible to reduce the functions for reading JSON values in this case

I have got a JSON format this way

{
    "items": {
        "item": {
            "Beverages": [
                {
                    "id": "0001",
                    "name": "Coke",
                    "image": {
                        "url": "json_images/coke.jpg",
                        "width": 200,
                        "height": 200
                    },
                    "category": [
                        {
                            "category_name": "Cokecan",
                            "image": "json_images/coke_can.jpg",
                            "type": [
                                {
                                    "id": "p1",
                                    "type": "250ml",
                                    "price": "50"
                                },
                                {
                                    "id": "p2",
                                    "type": "300ml",
                                    "price": "60"
                                }
                            ]
                        }
                    ]
                },
                {
                    "id": "0005",
                    "name": "Pepsi",
                    "image": {
                        "url": "json_images/pepsi.jpg",
                        "width": 200,
                        "height": 200
                    },
                    "category": [
                        {
                            "category_name": "Pepsican",
                            "image": "json_images/pepsi_can.jpg",
                            "type": [
                                {
                                    "id": "p4",
                                    "type": "250ml",
                                    "price": "50"
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    }
}

My question is ,if i select Beverages from the above JSON and then Coke , i am doing the following way to get the values 250ml and 300Ml

Is there anyway that i can do it more efficient way .

// function to return first level

function returnfirstlevelValues(value) {
                  var data;
                  $.each(jsondata.items.item, function(i, v) {
                    if (i == value) {

                      data = v;
                    }
                  });
 return data;
                }

// function to return second level

function returnSecondlevelValues(value) {
                  var innerdata = [];
                  for (var i = 0; i < retdata.length; i++) {
                    if (retdata[i].name == value) {
                      for (var b = 0; b < retdata[i].category.length; b++) {
                        innerdata.push(retdata[i].category[b].category_name);
                      }
                    }
                  }
                  return innerdata;
                }

// function to return third level

        function returnThirdlevelValues(value) {
          var objArray = [];
          $.each(retdata, function(i, item) {

            $.each(item.category, function(j, item) {

              if (item.category_name == value) {
                obj.image = item.image;
                obj.type = [];
                obj.price = [];
    obj.id = [];
                $.each(item.type, function(k, e) {
                  obj.type.push(e.type);
                  obj.price.push(e.price);
       obj.id.push(e.id);
                });
                objArray.push(obj);
              }
            });


          });
          return objArray;


        }

This is my jsfiddle http://jsfiddle.net/f5T9d/

您可以使用JQuery中的parseJSON,因此可以将数据视为对象

Handling JSON

This is how i would write it , it's old javascript & uses performant shorthands.

function first(value){
 return jsondata.items.item[value]||false
}

function second(value){
 var l=firstReturn.length,temp;
 while(l--){
  firstReturn[l].name!=value||(temp=firstReturn[l].category)
 };
 return temp||false
}

function third(value){
 var l=secondReturn.length,temp;
 while(l--){
  secondReturn[l]['category_name']!=value||(temp=secondReturn[l].type)
 };
 return temp||false
}

DEMO

http://jsfiddle.net/43MV2/

if the output is not what you expect just tell me and i make some modifications...

if you have any questions just ask.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM