如何从包含多个对象数组的Json数据中提取多个对象数组?

时间:2019-02-03 05:57:37

标签: javascript c# json model-view-controller

我有一个Json数据,其中包含mvc控制器发送的多个对象数组。我需要以不同的方式提取这些数组。我要分别设置 getEmailBasicData 数组, getEmailParticipantData 数组, getEmailDocumentData 数组该怎么做?这是我的代码-

"{
  "$id": "1",
  "getEmailBasicData": [
    {
      "$id": "2",
      "notificationId": 23,
      "subject": "final test",
      "message": "<p>this is body</p>\n",
      "sendStatus": -1,
      "lastSendTime": null
    },
    {
      "$id": "3",
      "notificationId": 24,
      "subject": "final subject",
      "message": "<p>final body</p>\n",
      "sendStatus": -1,
      "lastSendTime": null
    },
    {
      "$id": "12",
      "notificationId": 33,
      "subject": "Final Test",
      "message": "<p>Final body</p>\n",
      "sendStatus": -1,
      "lastSendTime": null
    }
  ],
  "getEmailParticipantData": [
    {
      "$id": "13",
      "id": 55,
      "notificationId": 23,
      "employeeId": -1,
      "name": "here is the name",
      "emailAddress": "whatever@gmail.com",
      "contactNo": null
    },
    {
      "$id": "14",
      "id": 56,
      "notificationId": 23,
      "employeeId": -1,
      "name": "another name",
      "emailAddress": "someone@gmail.com",
      "contactNo": null
    } ],
    "getEmailDocumentData": [
    {
      "$id": "36",
      "id": 40,
      "notificationId": 23,
      "docId": 1,
      "attachmentLocation": "1_1_50474603_2287746231437248_8529393497499762688_n.jpg"
    },
       {
      "$id": "46",
      "id": 50,
      "notificationId": 33,
      "docId": 1,
      "attachmentLocation": 
 "1_1_50474603_2287746231437248_8529393497499762688_n.jpg"
    }
  ]
}"

2 个答案:

答案 0 :(得分:1)

您可以使用destructuring assignment

let obj = {"$id":"1","getEmailBasicData":[{"$id":"2","notificationId":23,"subject":"finaltest","message":"<p>thisisbody</p>\n","sendStatus":-1,"lastSendTime":null},{"$id":"3","notificationId":24,"subject":"finalsubject","message":"<p>finalbody</p>\n","sendStatus":-1,"lastSendTime":null},{"$id":"12","notificationId":33,"subject":"FinalTest","message":"<p>Finalbody</p>\n","sendStatus":-1,"lastSendTime":null}],"getEmailParticipantData":[{"$id":"13","id":55,"notificationId":23,"employeeId":-1,"name":"MdRashedulIslam","emailAddress":"rashed.cse08@gmail.com","contactNo":null},{"$id":"14","id":56,"notificationId":23,"employeeId":-1,"name":"Akash","emailAddress":"akash@ravensystemstld.com","contactNo":null}],"getEmailDocumentData":[{"$id":"36","id":40,"notificationId":23,"docId":1,"attachmentLocation":"1_1_50474603_2287746231437248_8529393497499762688_n.jpg"},{"$id":"46","id":50,"notificationId":33,"docId":1,"attachmentLocation":"1_1_50474603_2287746231437248_8529393497499762688_n.jpg"}]}

const {getEmailBasicData,getEmailDocumentData,getEmailParticipantData} = obj

console.log(getEmailBasicData,getEmailDocumentData,getEmailParticipantData)

答案 1 :(得分:0)

使用.运算符访问

var a={
  "$id": "1",
  "getEmailBasicData": [
    {
      "$id": "2",
      "notificationId": 23,
      "subject": "final test",
      "message": "<p>this is body</p>\n",
      "sendStatus": -1,
      "lastSendTime": null
    },
    {
      "$id": "3",
      "notificationId": 24,
      "subject": "final subject",
      "message": "<p>final body</p>\n",
      "sendStatus": -1,
      "lastSendTime": null
    },
    {
      "$id": "12",
      "notificationId": 33,
      "subject": "Final Test",
      "message": "<p>Final body</p>\n",
      "sendStatus": -1,
      "lastSendTime": null
    }
  ],
  "getEmailParticipantData": [
    {
      "$id": "13",
      "id": 55,
      "notificationId": 23,
      "employeeId": -1,
      "name": "Md Rashedul Islam",
      "emailAddress": "rashed.cse08@gmail.com",
      "contactNo": null
    },
    {
      "$id": "14",
      "id": 56,
      "notificationId": 23,
      "employeeId": -1,
      "name": "Akash",
      "emailAddress": "akash@ravensystemstld.com",
      "contactNo": null
    } ],
    "getEmailDocumentData": [
    {
      "$id": "36",
      "id": 40,
      "notificationId": 23,
      "docId": 1,
      "attachmentLocation": "1_1_50474603_2287746231437248_8529393497499762688_n.jpg"
    },
       {
      "$id": "46",
      "id": 50,
      "notificationId": 33,
      "docId": 1,
      "attachmentLocation": 
 "1_1_50474603_2287746231437248_8529393497499762688_n.jpg"
    }
  ]
}
var basic=a.getEmailBasicData;
var doc=a.getEmailDocumentData;
var participants=a.getEmailParticipantData;
console.log(basic);
console.log(doc);
console.log(participants);