我正在尝试获取每个ID的所有 state 参数值- 2434,78657 。请帮助我在打字稿中进行操作。嵌套的JSON,我无法获取这些值
JSON请求:
{
"2434":[
{
"eventId":"90989",
"eventTime":"2019-12-11T11:20:53+04:00",
"eventType":"yyyy",
"event":{
"ServiceOrder":{
"priority":"1",
"state":"InProgress"}
}
},
{
"eventId":"6576",
"eventTime":"2019-12-11T11:20:53+04:00",
"eventType":"yyyy",
"event":{
"ServiceOrder":{
"priority":"1",
"state":"InProgress"
}
}
},
{
"eventId":"6576",
"eventTime":"2019-12-11T11:20:53+04:00",
"eventType":"yyyy",
"event":{
"ServiceOrder":{
"priority":"1",
"state":"Pending"
}
}
}
],
"78657":[
{
"eventId":"6576",
"eventTime":"2019-12-11T11:20:53+04:00",
"eventType":"yyyy",
"event":{
"ServiceOrder":{
"priority":"1",
"state":"Pending"
}
}
},
{
"eventId":"6576",
"eventTime":"2019-12-11T11:20:53+04:00",
"eventType":"yyyy",
"event":{
"ServiceOrder":{
"priority":"1",
"state":"Completed"
}
}
},
{
"eventId":"6576",
"eventTime":"2019-12-11T11:20:53+04:00",
"eventType":"yyyy",
"event":{
"ServiceOrder":{
"priority":"1",
"state":"Failed"
}
}
}
]
}
所需的输出:
例如,对于ID 2434,必须返回具有所有状态参数值的数组。
[进行中,进行中,待处理]
答案 0 :(得分:3)
这是您的必填答案,
此答案为 made dynamic
,以便您可以在 Object
中发送任意数量的键,并且它仍返回嵌套循环显示所有键的状态值。
可以为您带来神奇效果的代码
let object = {};
for (let key of Object.keys(test)) {
for (let value of test[key]) {
object[key] = object[key] || []
object[key].push(value['event']['ServiceOrder']['state']
)
}
}
代码执行:
let test = {
"2434":[
{
"eventId":"90989",
"eventTime":"2019-12-11T11:20:53+04:00",
"eventType":"yyyy",
"event":{
"ServiceOrder":{
"priority":"1",
"state":"InProgress"}
}
},
{
"eventId":"6576",
"eventTime":"2019-12-11T11:20:53+04:00",
"eventType":"yyyy",
"event":{
"ServiceOrder":{
"priority":"1",
"state":"InProgress"
}
}
},
{
"eventId":"6576",
"eventTime":"2019-12-11T11:20:53+04:00",
"eventType":"yyyy",
"event":{
"ServiceOrder":{
"priority":"1",
"state":"Pending"
}
}
}
],
"78657":[
{
"eventId":"6576",
"eventTime":"2019-12-11T11:20:53+04:00",
"eventType":"yyyy",
"event":{
"ServiceOrder":{
"priority":"1",
"state":"Pending"
}
}
},
{
"eventId":"6576",
"eventTime":"2019-12-11T11:20:53+04:00",
"eventType":"yyyy",
"event":{
"ServiceOrder":{
"priority":"1",
"state":"Completed"
}
}
},
{
"eventId":"6576",
"eventTime":"2019-12-11T11:20:53+04:00",
"eventType":"yyyy",
"event":{
"ServiceOrder":{
"priority":"1",
"state":"Failed"
}
}
}
]
}
let object = {};
for (let key of Object.keys(test)) {
for (let value of test[key]) {
object[key] = object[key] || []
object[key].push(
value['event']['ServiceOrder']['state']
)
}
}
console.log(object);
请运行上面的代码段
这是O / P:
{“ 2434”:[ “进行中”, “进行中”, “待处理”],“ 78657”:[ “待处理”, “已完成”, “失败”]}
答案 1 :(得分:0)
我认为这对您有帮助。 首先解码json。
$result = json_decode($row[1]);
echo $result->{'2434'}[0]->event->ServiceOrder->{'**state**'};
结果如下。
answer: InProgress
答案 2 :(得分:0)
选中此复选框,在这里您必须传递需要其身份的特定ID(例如2434)。 只需传递ID,它将以数组的形式返回状态。
function getStatusFromId(id: number){
return new Promise((resolve, reject) => {
let idObject = NotificationData[id];
let arrayOfStatus = new Array();
idObject.forEach((data) => {
arrayOfStatus.push(data.event.ServiceOrder["**state**"])
})
resolve(arrayOfStatus);
})
}
getStatusFromId(id)
.then((data) => {
console.log(data)
});
let NotificationData = {
"2434":[
{
"eventId":"90989",
"eventTime":"2019-12-11T11:20:53+04:00",
"eventType":"yyyy",
"event":{
"ServiceOrder":{
"priority":"1",
"**state**":"InProgress"}
}
},
{
"eventId":"6576",
"eventTime":"2019-12-11T11:20:53+04:00",
"eventType":"yyyy",
"event":{
"ServiceOrder":{
"priority":"1",
"**state**":"InProgress"
}
}
},
{
"eventId":"6576",
"eventTime":"2019-12-11T11:20:53+04:00",
"eventType":"yyyy",
"event":{
"ServiceOrder":{
"priority":"1",
"**state**":"Pending"
}
}
}
],
"78657":[
{
"eventId":"6576",
"eventTime":"2019-12-11T11:20:53+04:00",
"eventType":"yyyy",
"event":{
"ServiceOrder":{
"priority":"1",
"**state**":"Pending"
}
}
},
{
"eventId":"6576",
"eventTime":"2019-12-11T11:20:53+04:00",
"eventType":"yyyy",
"event":{
"ServiceOrder":{
"priority":"1",
"**state**":"Completed"
}
}
},
{
"eventId":"6576",
"eventTime":"2019-12-11T11:20:53+04:00",
"eventType":"yyyy",
"event":{
"ServiceOrder":{
"priority":"1",
"**state**":"Failed"
}
}
}
]
};