我有一个来自json数据的字符串如下: -
“订单号:123i产品很难找到标题名称超人产品代码:ABC01实例数量:1持续时间:10.91尺寸:0.0产品成本:$ 10.43产品代码:HELLO123实例数:1持续时间:0.91尺寸: 0.0产品成本:0.0美元“
我想将值分成数组!
需要帮助,我是jquery的新手,并尝试了其他选项!
答案 0 :(得分:2)
首先,您需要使用逗号正确格式化 json 字符串以分隔键值对,确保键名中没有空格并使用双引号括起非数字值。例如:
{
Order_Number : "123i the prodfuct is hard to found Title Name superman",
Products: [
{
Product_Code : "ABC01",
Number_Of_Instances : 1,
Duration: 10.91,
Size : 0.0,
Product_Cost : "$10.43"
},
{
Product_Code : "ABC02",
Number_Of_Instances : 2,
Duration: 5.91,
Size : 0.0,
Product_Cost : "$5.43"
}
]
}
在json字符串中包含此数据后,您可以将其解析为:
var data = JSON.parse(jsonString);
var orderNumber = data.Order_Number; // this is your order number
// this is how you iterate through the products in the order
for(product in data.Products) {
var code = product.Product_Code;
var instances = product.Number_Of_Instances;
// and so on..
}