json对象访问数据

问题描述 投票:1回答:3

我从php返回数据(使用json):

"[4, [{
    "quantity": "1",
    "name": "item 1",
    "price": "2",
    "currency": "GBP",
    "description": "item 1 description"
}, {
    "quantity": "1",
    "name": "item 1",
    "price": "2",
    "currency": "GBP",
    "description": "item 1 description"
}]]"

然后我如何访问数据。它曾用于返回使用第二部分:

    [{
        "quantity": "1",
        "name": "item 1",
        "price": "2",
        "currency": "GBP",
        "description": "item 1 description"
    }, {
        "quantity": "1",
        "name": "item 1",
        "price": "2",
        "currency": "GBP",
        "description": "item 1 description"
    }]

我使用jQuery.parseJson变成一个对象,但我似乎无法弄清楚如何在第二部分添加数字4

帮助会很棒

我的PHP代码是这样的

            $array['items'] = array();      
            $arrayOfMyNumbers = array();

            $total = 0;
            for( $i = 0; $i<2; $i++ ) {
                $foo = new StdClass();
                $foo->quantity = "1";
                $foo->name =  "item 1";
                $foo->price = "2";
                $foo->currency = "GBP";
                $foo->description = "item 1 description";;
                $arrayOfMyNumbers[] = $foo;
                $total += ($foo->price*$foo->quantity);
            }

            $returnArray = array();
            array_push($returnArray,$total,$arrayOfMyNumbers);

所以我以为我可以从javascript访问它:

            jsonArrayResponse =  (jsonObj);
            if(typeReq =="button"){
            console.log("return is "+(jsonArrayResponse));

但这是我如何得到4然后第二个数组部分,所以我可以将它转换为一个对象。

终于解决了!!!!

如果查看原始数据,您可以看到前导和尾随引号。这些导致json解码失败。所以我加入了一些正则表达式并且它有效:

            success: function (jsonObj) {
            console.log(jsonObj);
            jsonArrayResponse =  (jsonObj);
            var someStr = jsonObj.data.replace(/^"(.*)"$/, '$1');
            console.log("some str is "+someStr);

            parsed = JSON.parse(someStr);
            console.log("this should be 4"+parsed[1]); 

绝对是其中一个很容易错过的地方。感谢Devlin帮助我追踪它。

javascript php json
3个回答
0
投票

我如何访问数据?

你仍然会使用JSON.parse()。

var json = '[4, [{"quantity": "1","name": "item 1","price": "2","currency": "GBP","description": "item 1 description"}, {"quantity": "1","name": "item 1","price": "2","currency": "GBP","description": "item 1 description"}]]';

var parsed = JSON.parse(json);
console.log(parsed[0]);  //4
console.log(parsed[1][0].quantity);
console.log(parsed[1][1].quantity);

或者,换句话说:

for (var i = 0; i < parsed.length; i++) {
  console.log(parsed[i]);  //4
  for (var j = 0; j < parsed[i].length; j++) {
    var obj = parsed[i][j];
    console.log(obj);
    console.log(obj.quantity);
  }
}

但是:ぁzxswい


更新

OP表示存在关于“意外令牌,在位置1的JSON中”的错误。如果收到的响应不是字符串,则会发生这种情况。 JSON.stringify()将允许您使用JSON.parse()。

https://jsfiddle.net/zephyr_hex/2na2j317/

1
投票

如果你需要做的就是将var json2 = JSON.stringify([4, [{"quantity": "1","name": "item 1","price": "2","currency": "GBP","description": "item 1 description"}, {"quantity": "1","name": "item 1","price": "2","currency": "GBP","description": "item 1 description"}]]); var parsed2 = JSON.parse(json2); (在第一部分中)添加到第二个json对象,你只需要创建一个新数组,添加4,并添加第二个json对象:

4

0
投票

粗糙通行证:

var secondJsonObject = [{
        "quantity": "1",
        "name": "item 1",
        "price": "2",
        "currency": "GBP",
        "description": "item 1 description"
    }, {
        "quantity": "1",
        "name": "item 1",
        "price": "2",
        "currency": "GBP",
        "description": "item 1 description"
    }];

var newJsonObject = [4, secondJsonObject];

注意:无法保证您将按添加顺序获取数组值/对象。

© www.soinside.com 2019 - 2024. All rights reserved.