遍历 JSON 中具有相同属性的未知标识符(和未知数量)
Looping through unknown identifiers (and unknown amount) in JSON with same attributes inside
我正在使用 node.js 来编程。
这是一个 JSON 我将收到的简化示例:
{
"Transaction ID213": {
"drink": "milk",
"food": "eggs",
"mealType": "breakfast"
},
"Transaction ID432": {
"drink": "beer",
"food": "steak",
"mealType": "brunch"
},
"Transaction ID908": {
"drink": "water",
"food": "tacos",
"mealType": "dinner"
},
"Transaction ID776": {
"drink": "orange juice",
"food": "waffles",
"mealType": "breakfast"
}
}
我知道每个交易 ID 中的所有属性键。然而,我 will/do 不知道交易 ID 或我将收到多少个对象 (id)。示例中有 4 个对象 ID。最多可能有 20,000 人进入,也可能只有 1 人。数量未知。
对于每个交易 ID,我会将每个键的值发布到 api。我正在将该值映射到另一种 JSON 格式。例如:
{
"PROCESS_MEAL_Input": {
"LIQUID": TransactionID213.drink,
"SOLID": TransactionID213.food,
"TYPE": TransactionID213.mealType
}
}
我无法知道 TransactionID213,但我知道其中的密钥。
我想知道是否有办法使用 for 循环或其他循环结构来获取第一个对象(未知的 TransactionID213),POST 其中的属性,然后移动到下一个目的。继续此过程,直到 JSON 中不再有对象。谢谢。
也许您需要这样的东西?
var obj = {
"Transaction ID213": {
drink: "milk",
food: "eggs",
mealType: "breakfast"
},
"Transaction ID432": {
drink: "beer",
food: "steak",
mealType: "brunch"
},
"Transaction ID776": {
drink: "water",
food: "tacos",
mealType: "dinner"
},
"Transaction ID777": {
drink: "orange juice",
food: "waffles",
mealType: "breakfast"
}
};
var arr = [];
for (let p in obj) {
let newObj = {
PROCESS_MEAL_Input: {
LIQUID: obj[p].drink,
SOLID: obj[p].food,
TYPE: obj[p].mealType
}
};
//POST here. http.request(.... Or something
arr.push(newObj);
}
console.log(arr);
可能这样的事情会有所帮助:-
var jsonObject =
{
"Transaction ID213":
{
"drink": "milk",
"food": "eggs",
"mealType": "breakfast"
},
"Transaction ID432":
{
"drink": "beer",
"food": "steak",
"mealType": "brunch"
},
"Transaction ID776":
{
"drink": "water",
"food": "tacos",
"mealType": "dinner"
},
"Transaction ID776":
{
"drink": "orange juice",
"food": "waffles",
"mealType": "breakfast"
}
}
var userStr = JSON.stringify(jsonObject);
var ans = JSON.parse(userStr, (key, value) => {
if (typeof value === 'string') {
return value
}
return value;
});
console.log(ans);
希望对您有所帮助!
我正在使用 node.js 来编程。
这是一个 JSON 我将收到的简化示例:
{
"Transaction ID213": {
"drink": "milk",
"food": "eggs",
"mealType": "breakfast"
},
"Transaction ID432": {
"drink": "beer",
"food": "steak",
"mealType": "brunch"
},
"Transaction ID908": {
"drink": "water",
"food": "tacos",
"mealType": "dinner"
},
"Transaction ID776": {
"drink": "orange juice",
"food": "waffles",
"mealType": "breakfast"
}
}
我知道每个交易 ID 中的所有属性键。然而,我 will/do 不知道交易 ID 或我将收到多少个对象 (id)。示例中有 4 个对象 ID。最多可能有 20,000 人进入,也可能只有 1 人。数量未知。
对于每个交易 ID,我会将每个键的值发布到 api。我正在将该值映射到另一种 JSON 格式。例如:
{
"PROCESS_MEAL_Input": {
"LIQUID": TransactionID213.drink,
"SOLID": TransactionID213.food,
"TYPE": TransactionID213.mealType
}
}
我无法知道 TransactionID213,但我知道其中的密钥。
我想知道是否有办法使用 for 循环或其他循环结构来获取第一个对象(未知的 TransactionID213),POST 其中的属性,然后移动到下一个目的。继续此过程,直到 JSON 中不再有对象。谢谢。
也许您需要这样的东西?
var obj = {
"Transaction ID213": {
drink: "milk",
food: "eggs",
mealType: "breakfast"
},
"Transaction ID432": {
drink: "beer",
food: "steak",
mealType: "brunch"
},
"Transaction ID776": {
drink: "water",
food: "tacos",
mealType: "dinner"
},
"Transaction ID777": {
drink: "orange juice",
food: "waffles",
mealType: "breakfast"
}
};
var arr = [];
for (let p in obj) {
let newObj = {
PROCESS_MEAL_Input: {
LIQUID: obj[p].drink,
SOLID: obj[p].food,
TYPE: obj[p].mealType
}
};
//POST here. http.request(.... Or something
arr.push(newObj);
}
console.log(arr);
可能这样的事情会有所帮助:-
var jsonObject =
{
"Transaction ID213":
{
"drink": "milk",
"food": "eggs",
"mealType": "breakfast"
},
"Transaction ID432":
{
"drink": "beer",
"food": "steak",
"mealType": "brunch"
},
"Transaction ID776":
{
"drink": "water",
"food": "tacos",
"mealType": "dinner"
},
"Transaction ID776":
{
"drink": "orange juice",
"food": "waffles",
"mealType": "breakfast"
}
}
var userStr = JSON.stringify(jsonObject);
var ans = JSON.parse(userStr, (key, value) => {
if (typeof value === 'string') {
return value
}
return value;
});
console.log(ans);
希望对您有所帮助!