我有json对象,但我无法访问' name'这个元素?

时间:2016-06-23 09:00:41

标签: jquery json

这是json数据

{
    "0": {
        "restaurant": "Bayleaf",
        "order": "HIHUN10",
        "delivery at": "04:43 PM 2016-06-22",
        "type": "Home Delivery",
        "name": "dEEPAK rAI",
        "address": "12356,Lucknow,226010",
        "phone": "9120276440",
        "total": "Rs 831.60",
        "pay by": "Cash On Delivery",
        "picked by": "By Administrator",
        "date": "06\/22\/2016",
        "status": "Waiting",
        "action": "pending"
    },
    "1": {
        "restaurant": "Jungliee",
        "order": "HIHUN9",
        "delivery at": "03:40 PM 2016-06-21",
        "type": "Home Delivery",
        "name": "dEEPAK rAI",
        "address": "12356,Lucknow,226010",
        "phone": "9120276440",
        "total": "Rs 465.00",
        "pay by": "Cash On Delivery",
        "picked by": "Dummy",
        "date": "06\/21\/2016",
        "status": {
            "Accepted": 1,
            "Processing": 2,
            "Delivered": 3,
            "Completed": 4,
            "Failed": 5
        },
        "action": "view"
    },
    "2": {
        "restaurant": "Naushijaan",
        "order": "HIHUN8",
        "delivery at": "11:10 AM 2016-06-18",
        "type": "Home Delivery",
        "name": "dEEPAK rAI",
        "address": "12356,Lucknow,226010",
        "phone": "9120276440",
        "total": "Rs 545.00",
        "pay by": "Cash On Delivery",
        "picked by": "By Administrator",
        "date": "06\/18\/2016",
        "status": {
            "Processing": 2,
            "Delivered": 3,
            "Completed": 4,
            "Failed": 5
        },
        "action": "view"
    },
    "3": {
        "restaurant": "Aahaar",
        "order": "HIHUN7",
        "delivery at": "11:32 AM 2016-06-12",
        "type": "Home Delivery",
        "name": "sk mishra",
        "address": "520viram khand,Lucknow,226010",
        "phone": "9198979962",
        "total": "Rs 450.00",
        "pay by": "Cash On Delivery",
        "picked by": "Dummy",
        "date": "06\/12\/2016",
        "status": {
            "Completed": 4,
            "Failed": 5
        },
        "action": "view"
    },
    "4": {
        "restaurant": "Bayleaf",
        "order": "HIHUN6",
        "delivery at": "08:29 PM 2016-06-11",
        "type": "Home Delivery",
        "name": "Dharmraj",
        "address": "D-242Office G 19 Sector 63,Lucknow,201003",
        "phone": "7428069025",
        "total": "Rs 606.60",
        "pay by": "Cash On Delivery",
        "picked by": "By Administrator",
        "date": "06\/11\/2016",
        "status": {
            "Completed": 4,
            "Failed": 5
        },
        "action": "view"
    },
    "5": {
        "restaurant": "Bayleaf",
        "order": "HIHUN5",
        "delivery at": "08:17 PM 2016-06-11",
        "type": "Home Delivery",
        "name": "Dharmraj",
        "address": "D-242Office G 19 Sector 63,Lucknow,201003",
        "phone": "7428069025",
        "total": "Rs 606.60",
        "pay by": "Cash On Delivery",
        "picked by": "By Administrator",
        "date": "06\/11\/2016",
        "status": {
            "Completed": 4,
            "Failed": 5
        },
        "action": "view"
    }
}

6 个答案:

答案 0 :(得分:1)

这是您访问名称字段的方法。



var yourJson = {"0":{"restaurant":"Bayleaf","order":"HIHUN10","delivery at":"04:43 PM 2016-06-22","type":"Home Delivery","name":"dEEPAK rAI","address":"12356,Lucknow,226010","phone":"9120276440","total":"Rs 831.60","pay by":"Cash On Delivery","picked by":"By Administrator","date":"06\/22\/2016","status":"Waiting","action":"pending"},"1":{"restaurant":"Jungliee","order":"HIHUN9","delivery at":"03:40 PM 2016-06-21","type":"Home Delivery","name":"dEEPAK rAI","address":"12356,Lucknow,226010","phone":"9120276440","total":"Rs 465.00","pay by":"Cash On Delivery","picked by":"Dummy","date":"06\/21\/2016","status":{"Accepted":1,"Processing":2,"Delivered":3,"Completed":4,"Failed":5},"action":"view"},"2":{"restaurant":"Naushijaan","order":"HIHUN8","delivery at":"11:10 AM 2016-06-18","type":"Home Delivery","name":"dEEPAK rAI","address":"12356,Lucknow,226010","phone":"9120276440","total":"Rs 545.00","pay by":"Cash On Delivery","picked by":"By Administrator","date":"06\/18\/2016","status":{"Processing":2,"Delivered":3,"Completed":4,"Failed":5},"action":"view"},"3":{"restaurant":"Aahaar","order":"HIHUN7","delivery at":"11:32 AM 2016-06-12","type":"Home Delivery","name":"sk mishra","address":"520viram khand,Lucknow,226010","phone":"9198979962","total":"Rs 450.00","pay by":"Cash On Delivery","picked by":"Dummy","date":"06\/12\/2016","status":{"Completed":4,"Failed":5},"action":"view"},"4":{"restaurant":"Bayleaf","order":"HIHUN6","delivery at":"08:29 PM 2016-06-11","type":"Home Delivery","name":"Dharmraj","address":"D-242Office G 19 Sector 63,Lucknow,201003","phone":"7428069025","total":"Rs 606.60","pay by":"Cash On Delivery","picked by":"By Administrator","date":"06\/11\/2016","status":{"Completed":4,"Failed":5},"action":"view"},"5":{"restaurant":"Bayleaf","order":"HIHUN5","delivery at":"08:17 PM 2016-06-11","type":"Home Delivery","name":"Dharmraj","address":"D-242Office G 19 Sector 63,Lucknow,201003","phone":"7428069025","total":"Rs 606.60","pay by":"Cash On Delivery","picked by":"By Administrator","date":"06\/11\/2016","status":{"Completed":4,"Failed":5},"action":"view"}}
console.log(yourJson["0"].name);
console.log(yourJson["1"].name);  //etc.




答案 1 :(得分:0)

如果它已经是json的对象,只需访问它:

var data = {"0":{"restaurant":"Bayleaf","order":"HIHUN10","delivery at":"04:43 PM 2016-06-22","type":"Home Delivery","name":"dEEPAK rAI","address":"12356,Lucknow,226010","phone":"9120276440","total":"Rs 831.60","pay by":"Cash On Delivery","picked by":"By Administrator","date":"06\/22\/2016","status":"Waiting","action":"pending"},"1":{"restaurant":"Jungliee","order":"HIHUN9","delivery at":"03:40 PM 2016-06-21","type":"Home Delivery","name":"dEEPAK rAI","address":"12356,Lucknow,226010","phone":"9120276440","total":"Rs 465.00","pay by":"Cash On Delivery","picked by":"Dummy","date":"06\/21\/2016","status":{"Accepted":1,"Processing":2,"Delivered":3,"Completed":4,"Failed":5},"action":"view"},"2":{"restaurant":"Naushijaan","order":"HIHUN8","delivery at":"11:10 AM 2016-06-18","type":"Home Delivery","name":"dEEPAK rAI","address":"12356,Lucknow,226010","phone":"9120276440","total":"Rs 545.00","pay by":"Cash On Delivery","picked by":"By Administrator","date":"06\/18\/2016","status":{"Processing":2,"Delivered":3,"Completed":4,"Failed":5},"action":"view"},"3":{"restaurant":"Aahaar","order":"HIHUN7","delivery at":"11:32 AM 2016-06-12","type":"Home Delivery","name":"sk mishra","address":"520viram khand,Lucknow,226010","phone":"9198979962","total":"Rs 450.00","pay by":"Cash On Delivery","picked by":"Dummy","date":"06\/12\/2016","status":{"Completed":4,"Failed":5},"action":"view"},"4":{"restaurant":"Bayleaf","order":"HIHUN6","delivery at":"08:29 PM 2016-06-11","type":"Home Delivery","name":"Dharmraj","address":"D-242Office G 19 Sector 63,Lucknow,201003","phone":"7428069025","total":"Rs 606.60","pay by":"Cash On Delivery","picked by":"By Administrator","date":"06\/11\/2016","status":{"Completed":4,"Failed":5},"action":"view"},"5":{"restaurant":"Bayleaf","order":"HIHUN5","delivery at":"08:17 PM 2016-06-11","type":"Home Delivery","name":"Dharmraj","address":"D-242Office G 19 Sector 63,Lucknow,201003","phone":"7428069025","total":"Rs 606.60","pay by":"Cash On Delivery","picked by":"By Administrator","date":"06\/11\/2016","status":{"Completed":4,"Failed":5},"action":"view"}};

// as single, hardcoded entry
var nameAt0 = data[0].name;
var nameAt1 = data[1].name;
...

// as loop
for( var i in data )
    if( data.hasOwnProperty(i) )
        var name = data[i].name;

答案 2 :(得分:0)

您的json是一个对象,要访问您必须遍历属性的名称:

$.each(data, function(i, e) {
  alert(e.name);
});

https://jsfiddle.net/kj0azubb/

var data = {
  "0": {
    "restaurant": "Bayleaf",
    "order": "HIHUN10",
    "delivery at": "04:43 PM 2016-06-22",
    "type": "Home Delivery",
    "name": "dEEPAK rAI",
    "address": "12356,Lucknow,226010",
    "phone": "9120276440",
    "total": "Rs 831.60",
    "pay by": "Cash On Delivery",
    "picked by": "By Administrator",
    "date": "06\/22\/2016",
    "status": "Waiting",
    "action": "pending"
  },
  "1": {
    "restaurant": "Jungliee",
    "order": "HIHUN9",
    "delivery at": "03:40 PM 2016-06-21",
    "type": "Home Delivery",
    "name": "dEEPAK rAI",
    "address": "12356,Lucknow,226010",
    "phone": "9120276440",
    "total": "Rs 465.00",
    "pay by": "Cash On Delivery",
    "picked by": "Dummy",
    "date": "06\/21\/2016",
    "status": {
      "Accepted": 1,
      "Processing": 2,
      "Delivered": 3,
      "Completed": 4,
      "Failed": 5
    },
    "action": "view"
  },
  "2": {
    "restaurant": "Naushijaan",
    "order": "HIHUN8",
    "delivery at": "11:10 AM 2016-06-18",
    "type": "Home Delivery",
    "name": "dEEPAK rAI",
    "address": "12356,Lucknow,226010",
    "phone": "9120276440",
    "total": "Rs 545.00",
    "pay by": "Cash On Delivery",
    "picked by": "By Administrator",
    "date": "06\/18\/2016",
    "status": {
      "Processing": 2,
      "Delivered": 3,
      "Completed": 4,
      "Failed": 5
    },
    "action": "view"
  },
  "3": {
    "restaurant": "Aahaar",
    "order": "HIHUN7",
    "delivery at": "11:32 AM 2016-06-12",
    "type": "Home Delivery",
    "name": "sk mishra",
    "address": "520viram khand,Lucknow,226010",
    "phone": "9198979962",
    "total": "Rs 450.00",
    "pay by": "Cash On Delivery",
    "picked by": "Dummy",
    "date": "06\/12\/2016",
    "status": {
      "Completed": 4,
      "Failed": 5
    },
    "action": "view"
  },
  "4": {
    "restaurant": "Bayleaf",
    "order": "HIHUN6",
    "delivery at": "08:29 PM 2016-06-11",
    "type": "Home Delivery",
    "name": "Dharmraj",
    "address": "D-242Office G 19 Sector 63,Lucknow,201003",
    "phone": "7428069025",
    "total": "Rs 606.60",
    "pay by": "Cash On Delivery",
    "picked by": "By Administrator",
    "date": "06\/11\/2016",
    "status": {
      "Completed": 4,
      "Failed": 5
    },
    "action": "view"
  },
  "5": {
    "restaurant": "Bayleaf",
    "order": "HIHUN5",
    "delivery at": "08:17 PM 2016-06-11",
    "type": "Home Delivery",
    "name": "Dharmraj",
    "address": "D-242Office G 19 Sector 63,Lucknow,201003",
    "phone": "7428069025",
    "total": "Rs 606.60",
    "pay by": "Cash On Delivery",
    "picked by": "By Administrator",
    "date": "06\/11\/2016",
    "status": {
      "Completed": 4,
      "Failed": 5
    },
    "action": "view"
  }
};

$.each(data, function(i, e) {
  $("#content").append(e.name);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="content">
</div>

答案 3 :(得分:0)

var jsonData = {
  "0": {
    "restaurant": "Bayleaf",
    "order": "HIHUN10",
    "delivery at": "04:43 PM 2016-06-22",
    "type": "Home Delivery",
    "name": "dEEPAK rAI",
    "address": "12356,Lucknow,226010",
    "phone": "9120276440",
    "total": "Rs 831.60",
    "pay by": "Cash On Delivery",
    "picked by": "By Administrator",
    "date": "06\/22\/2016",
    "status": "Waiting",
    "action": "pending"
  },
  "1": {
    "restaurant": "Jungliee",
    "order": "HIHUN9",
    "delivery at": "03:40 PM 2016-06-21",
    "type": "Home Delivery",
    "name": "dEEPAK rAI",
    "address": "12356,Lucknow,226010",
    "phone": "9120276440",
    "total": "Rs 465.00",
    "pay by": "Cash On Delivery",
    "picked by": "Dummy",
    "date": "06\/21\/2016",
    "status": {
      "Accepted": 1,
      "Processing": 2,
      "Delivered": 3,
      "Completed": 4,
      "Failed": 5
    },
    "action": "view"
  },
  "2": {
    "restaurant": "Naushijaan",
    "order": "HIHUN8",
    "delivery at": "11:10 AM 2016-06-18",
    "type": "Home Delivery",
    "name": "dEEPAK rAI",
    "address": "12356,Lucknow,226010",
    "phone": "9120276440",
    "total": "Rs 545.00",
    "pay by": "Cash On Delivery",
    "picked by": "By Administrator",
    "date": "06\/18\/2016",
    "status": {
      "Processing": 2,
      "Delivered": 3,
      "Completed": 4,
      "Failed": 5
    },
    "action": "view"
  },
  "3": {
    "restaurant": "Aahaar",
    "order": "HIHUN7",
    "delivery at": "11:32 AM 2016-06-12",
    "type": "Home Delivery",
    "name": "sk mishra",
    "address": "520viram khand,Lucknow,226010",
    "phone": "9198979962",
    "total": "Rs 450.00",
    "pay by": "Cash On Delivery",
    "picked by": "Dummy",
    "date": "06\/12\/2016",
    "status": {
      "Completed": 4,
      "Failed": 5
    },
    "action": "view"
  },
  "4": {
    "restaurant": "Bayleaf",
    "order": "HIHUN6",
    "delivery at": "08:29 PM 2016-06-11",
    "type": "Home Delivery",
    "name": "Dharmraj",
    "address": "D-242Office G 19 Sector 63,Lucknow,201003",
    "phone": "7428069025",
    "total": "Rs 606.60",
    "pay by": "Cash On Delivery",
    "picked by": "By Administrator",
    "date": "06\/11\/2016",
    "status": {
      "Completed": 4,
      "Failed": 5
    },
    "action": "view"
  },
  "5": {
    "restaurant": "Bayleaf",
    "order": "HIHUN5",
    "delivery at": "08:17 PM 2016-06-11",
    "type": "Home Delivery",
    "name": "Dharmraj",
    "address": "D-242Office G 19 Sector 63,Lucknow,201003",
    "phone": "7428069025",
    "total": "Rs 606.60",
    "pay by": "Cash On Delivery",
    "picked by": "By Administrator",
    "date": "06\/11\/2016",
    "status": {
      "Completed": 4,
      "Failed": 5
    },
    "action": "view"
  }
};
$.each(jsonData, function(index, value) {
  console.log(JSON.stringify(value.name));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

答案 4 :(得分:0)

现在它的工作正常,每个答案都是正确的。我错过了

 dataType: 'json'
   ,

'dataType:json'在ajax块中。<​​/ p>

答案 5 :(得分:0)

首先,您有一个非标准的JSON结构,但是,您无法作为JSON数组进行访问,因此,您必须将JSON解析为可以更轻松地阅读的对象。

private static void OnContextMenuOpening(object parameter)
{
  CommandManager.InvalidateRequerySuggested();
}

然后你可以使用它

    var MyJSONArray = [];
    $.each(YourJSON,function(index,e){
        MyJSONArray[] = e;
    ]

就是这样! 享受