我有json对象,但我无法访问此中的“name”元素?

a5g8bdjr  于 2023-03-13  发布在  其他
关注(0)|答案(6)|浏览(135)

这是JSON数据。当我解析它时它不起作用。但是现在它起作用了。
{“2”:{“餐厅”:“瑙什贾安”,“命令”:“HIHUN 8”,“交货地点”:“2016年6月18日上午11:10”,“类型”:“送货上门”,“姓名”:“abc ddd”,“地址”:“12356,勒克瑙,226010”,“电话”:“6777779999”,“总计”:“545.00卢比”,“付款人”:“货到付款”、“提货人”:“管理员”,“日期”:“2016年6月18日”,“状态”:{“处理”:2、“已交付”:3、“已完成”:4、“未通过”:5 }、“动作”:“视图”},“3”:{“餐厅”:“阿哈尔”,“秩序”:“HIHUN 7”,“交货地点”:“2016年6月12日上午11:32”,“类型”:“送货上门”,“姓名”:“sk米什拉”,“地址”:“勒克瑙520 viram khand,226010”,“电话”:“679999999”,“总计”:“450.00卢比”,“付款人”:“货到付款”、“提货人”:“仿制品”、“日期”:“2016年6月12日”,“状态”:{“已完成”:4、“未通过”:5 }、“动作”:“视图”},

}
snvhrwxg

snvhrwxg1#

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

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

cbwuti442#

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>
avkwfej4

avkwfej43#

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

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

https://jsfiddle.net/kj0azubb/
x一个一个一个一个x一个一个二个x

qxsslcnc

qxsslcnc4#

如果它已经是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;
c86crjj0

c86crjj05#

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

dataType: 'json'
   ,

AJAX 块中的“dataType:json”。

rur96b6h

rur96b6h6#

首先,您有一个非常不标准的JSON结构,但是,您不能作为JSON数组访问,因此,您必须将JSON解析为一个更容易阅读的对象。

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

然后你可以把它当作

var restarant_1 = MyJSONArray[0].restaurant;

就是这个!好好享受吧

相关问题