Javascript 循环解析JSON数据

Javascript 循环解析JSON数据,javascript,json,Javascript,Json,说明和目标: 从本质上讲,数据每隔2分钟就会生成一次JSON数据。我需要做的是从提供的JSON数据中检索信息。数据将不断变化。一旦解析了信息,就需要将其捕获到可以在其他函数中使用的变量中 我所陷入的困境是试图找出如何创建一个带有循环的函数,该循环将所有数据重新分配给存储的变量,这些变量以后可以在函数中使用 示例信息: var json = {"data": {"shop":[ { "carID":"7", "Garage":"7", "Mechanic":"Michael Jamison", "

说明和目标: 从本质上讲,数据每隔2分钟就会生成一次JSON数据。我需要做的是从提供的JSON数据中检索信息。数据将不断变化。一旦解析了信息,就需要将其捕获到可以在其他函数中使用的变量中

我所陷入的困境是试图找出如何创建一个带有循环的函数,该循环将所有数据重新分配给存储的变量,这些变量以后可以在函数中使用

示例信息:

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.
}

shop[1]={}

好吧,您的输出示例是不可能的。你有一个列表,但是你使用的是对象语法

如果您真的希望这些项采用列表格式而不是键值对,那么有意义的是:

shop[0]=[7,7,"Michael Jamison",repair,"Blown Head gasket and two rail mounts", 07/22/2011,00:02:18]
要循环对象中的属性,可以使用以下内容:

shop[0]={7,7,"Michael Jamison",repair,"Blown Head gasket and two rail mounts", 07/22/2011,00:02:18  }
var properties = Array();
for (var propertyName in theObject) {
  // Check if it’s NOT a function
  if (!(theObject[propertyName] instanceof Function)) {
    properties.push(propertyName);
  }
}

老实说,我真的不知道你为什么要把它放在一个不同的格式。json数据已经达到了它所能达到的水平,您可以使用shop[0][“carID”]来获取该字段中的数据。

您可以使用以下代码循环json字符串

      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
            }
         }
var JSONstring=[{“key1”:“value1”,“key2”:“value2”},{“key3”:“value3”}];

对于(var i=0;i,我觉得您想要提取JSON对象的“shop”属性中的数据,以便可以轻松地引用该商店的所有项目。以下是一个示例:

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);
var-json=
{
“数据”:
{“商店”:
[
{“itemName”:“car”,“price”:30000},
{“项目名称”:“车轮”,“价格”:500}
]
}
},
存货=[];
//将商店的库存映射到我们的库存阵列。
对于(var i=0,j=json.data.shop.length;i
是否有办法将这些信息转换成其他可用的变量。请给我指出正确的方向。本质上,我需要将一个事物列表返回到一个对象中。我在第二块中给出的代码确实会将一个事物列表放入数组中。您应该看看真正简单的