好吧,这个问题可能有点难。我不是一个资深的 JSON 用户,但无论如何我们还是要开始..
我有一个巨大的视频游戏项目文件。问题是,JSON 的格式化方式使得数据很难调用。
示例:
"@uniquename": "T3_MEAL_SOUP",
"@weight": "3.15",
"@nutrition": "252",
"craftingrequirements": {
"@craftingfocus": "168",
"craftresource": {
"@uniquename": "T3_WHEAT",
"@count": "48"
}
},
"enchantments": {
"enchantment": [
{
"@enchantmentlevel": "1",
"craftingrequirements": {
"@craftingfocus": "235",
"craftresource": [
{
"@uniquename": "T3_WHEAT",
"@count": "48"
},
{
"@uniquename": "T1_FISHSAUCE_LEVEL1",
"@count": "30"
}
]
}
},
{
"@enchantmentlevel": "2",
"craftingrequirements": {
"@craftingfocus": "368",
"craftresource": [
{
"@uniquename": "T3_WHEAT",
"@count": "48"
},
{
"@uniquename": "T1_FISHSAUCE_LEVEL2",
"@count": "30"
}
]
}
},
{
"@enchantmentlevel": "3",
"craftingrequirements": {
"@craftingfocus": "769",
"craftresource": [
{
"@uniquename": "T3_WHEAT",
"@count": "48"
},
{
"@uniquename": "T1_FISHSAUCE_LEVEL3",
"@count": "30"
}
]
}
}
]
}
},
我正在尝试使用 Excel 中的 Power Query 或 Google 表格中的“ImportJSON”来从此 JSON 获取数据。
我需要这些“配方”是唯一的,文件的最终版本应该像这样安装。
{
"@uniquename": "T3_MEAL_SOUP",
"@weight": "3.15",
"@nutrition": "252",
"craftingrequirements": {
"@craftingfocus": "168",
"craftresource": {
"@uniquename": "T3_WHEAT",
"@count": "48"
}
},
{
"@uniquename": "T3_MEAL_SOUP@1",
"@weight": "3.15",
"@nutrition": "252",
"craftingrequirements": {
"@craftingfocus": "235",
"craftresource": {
"@uniquename": "T3_WHEAT",
"@count": "48",
"@uniquename1": "T1_FISHSAUCE_LEVEL1",
"@count1": "30"
},
{
"@uniquename": "T3_MEAL_SOUP@2",
"@weight": "3.15",
"@nutrition": "252",
"craftingrequirements": {
"@craftingfocus": "368",
"craftresource": {
"@uniquename": "T3_WHEAT",
"@count": "48",
"@uniquename1": "T1_FISHSAUCE_LEVEL2",
"@count1": "30"
}
}}},
基本上,它采用“Enchantments”对象并将其完全转换为一个新对象,因此它不会嵌套在第一个对象中。给它一个新名称“T3_MEAL_SOUP@1”并在下面添加食谱。
我在导入 Excel 和 Google Sheets 时遇到的问题是,因为某些项目只有 1 个“craftresource”,而其他项目在数组中最多有 4 个项目,因此数据确实很难一致获取。
我希望有人能帮助我解决我的问题。
我已经在这里上传了完整的原始文件,所以可能更容易理解我想要做什么..
https://raw.githubusercontent.com/rasmuslhornung/AlbionData/main/Food_Formatted.json
建议:
您可以使用 Google Apps 脚本将魔咒与 JSON 文件的其他属性分开,如下所示:
示例代码:
function myFunction() {
var data = UrlFetchApp.fetch('https://raw.githubusercontent.com/rasmuslhornung/AlbionData/main/Food_Formatted.json').getContentText();
var json = JSON.parse(data);
var enchantment = json.filter(x => x.enchantments).map(y => y.enchantments);
for (const property in json){
if(json[property].enchantments != undefined ){
delete json[property].enchantments
}
}
json.map(x => console.log(x['@uniquename']));
console.log(json);
console.log(enchantment);
}
enchantment
变量存储所有附魔对象,json
变量存储其余的。
此外,似乎不需要为每个
uniquename
属性指定新名称,因为所有这些名称都已经是唯一的。
参考资料:
https://developers.google.com/apps-script/reference/url-fetchhttps://www.w3schools.com/howto/howto_js_remove_property_object.asphttps://developer.mozilla.org/ en-US/docs/Web/JavaScript/Reference/Statements/for...in