json对象访问数据

时间:2017-12-20 20:06:30

标签: javascript php json

我已从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帮助我追踪它。

3 个答案:

答案 0 :(得分:1)

如果您需要做的只是将4(在第一部分中)添加到第二个json对象,您只需创建一个新数组,添加4,然后添加第二个json对象:

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];

答案 1 :(得分:0)

  

如何访问数据?

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

Subject

或者,换句话说:

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);

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

<强>更新

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

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);
  }
}

答案 2 :(得分:0)

粗略传球:

ALTER TRIGGER [dbo].[deleted] 
ON [dbo].[DispTech]           
FOR DELETE, INSERT
AS
    SET NOCOUNT ON;

    IF (SELECT serviceman FROM deleted) <> (SELECT ServiceMan FROM inserted)
    BEGIN
        INSERT INTO misc.dbo.DeletedTest ("Status", dispatch, serviceman)     
            SELECT
                'Deleted', d.Dispatch, d.ServiceMan 
            FROM
                deleted d

       INSERT INTO misc.dbo.DeletedTest ("Status", dispatch, serviceman) 
           SELECT
               'Inserted', i.Dispatch, i.ServiceMan 
           FROM
               inserted i
END 

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