开发者

Looping to Parse JSON Data

Description and Goal: Essentially data is constantly generated every 2 minutes into JSON data. What I need to do is retrieve the information from the supplied JSON data. The data will changed constantly. Once the information is parsed it needs to be captured into variables that can be used in other functions.

What I am stuck in is trying to figure out how to create a function with a loop that reassigns all of the data to stored variables that can later be used in functions.

Example information:

var json = {"data":
{"shop":[
{
"carID":"7",
"Garage":"7",
"Mechanic":"Michael Jamison",
"notificationsType":"repair",
"notificationsDesc":"Blown Head gasket and two rail mounts开发者_开发知识库",
"notificationsDate":07/22/2011,
"notificationsTime":"00:02:18"
},

{
"CarID":"8",
"Garage":"7",
"Mechanic":"Tom Bennett",
"notificationsType":"event",
"notifications":"blown engine, 2 tires, and safety inspection",
"notificationsDate":"16 April 2008",
"notificationsTime":"08:26:24"
}
]
}};

function GetInformationToReassign(){
var i;
for(i=0; i<json.data.shop.length; i++)
{
 //Then the data is looped, stored into multi-dimensional arrays that can be indexed.
}

}

So the ending result needs to be like this:

shop[0]={7,7,"Michael Jamison",repair,"Blown Head gasket and two rail mounts", 07/22/2011,00:02:18  }

shop[1]={}


You can loop through your JSON string using the following code,

      var JSONstring=[{"key1":"value1","key2":"value2"},{"key3":"value3"}];

        for(var i=0;i<JSONstring.length;i++){
        var obj = JSONstring[i];
          for(var key in obj){
                 var attrName = key;
                 var attrValue = obj[key];

                //based on the result create as you need
            }
         }

Hope this helps...


It sounds to me like you want to extract the data in the "shop" property of the JSON object so that you can easily reference all of the shop's items. Here is an example:

var json =
  {
    "data":
      {"shop":
        [
          {"itemName":"car", "price":30000},
          {"itemName":"wheel", "price":500}
        ]
      }
  },
  inventory = [];

// Map the shop's inventory to our inventory array.
for (var i = 0, j = json.data.shop.length; i < j; i += 1) {
  inventory[i] = json.data.shop[i];
}

// Example of using our inventory array
console.log( inventory[0].itemName + " has a price of $" + inventory[0].price);


Well, your output example is not possible. You have what is a list of things, but you're using object syntax.

What would instead make sense if you really want those items in a list format instead of key-value pairs would be this:

shop[0]=[7,7,"Michael Jamison",repair,"Blown Head gasket and two rail mounts", 07/22/2011,00:02:18]

For looping through properties in an object you can use something like this:

var properties = Array();
for (var propertyName in theObject) {
  // Check if it’s NOT a function
  if (!(theObject[propertyName] instanceof Function)) {
    properties.push(propertyName);
  }
}

Honestly though, I'm not really sure why you'd want to put it in a different format. The json data already is about as good as it gets, you can do shop[0]["carID"] to get the data in that field.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜