我有一个本地JSON文件的工作,并尝试从它使某些节点。为什么我问它在计算器上的原因,然而,就是在JSON文件本身在很大程度上嵌套一吨的子节点,我不知道如何遍历像这样的一个JSON文件。我扁平的数据,以试图缓解这个问题,但它似乎没有做多好。
我想专门访问__text
(即__text": "Guides & Protocols"
)
{
"feed": {
"id": "[redacted]",
"title": "",
"updated": "2019-01-17T21:01:03Z",
"entry": [
{
"id": "Web/Lists(guid'[redacted]')/Items(1)",
"category": {
"_term": "[redacted]",
"_scheme": "[redacted url]"
},
"link": {
"_rel": "edit",
"_href": "Web/Lists(guid'[redacted]')/Items(1)"
},
"title": "",
"updated": "2019-01-17T21:01:03Z",
"author": {
"name": ""
},
"content": {
"properties": {
"ResourceType": {
"element": {
"Label": {
"__prefix": "d",
"__text": "Guides & Protocols" <--------------------
},
"TermGuid": {
"__prefix": "d",
"__text": "[redacted]"
},
"WssId": {
"_m:type": "[redacted]",
"__prefix": "d",
"__text": "706"
},
"__prefix": "d"
},
"_m:type": "Collection([redacted])",
"__prefix": "d"
},
"__prefix": "m"
},
"_type": "application/xml"
},
"_m:etag": "\"2\""
}
...
]
}
import $ from 'jquery';
import axios from 'axios';
import myJSONfile from '../../../public/myJSONfile.json';
import tinyJsonFlat from '../../../public/tinyJsonFlat.json'; // Top 10, ResourceTypes only
import { basename } from 'path';
export default class {
constructor() {
$('<div class="test-div">testing</div>').appendTo(document.body);
this.loadData();
}
loadData() {
var data = tinyJsonFlat // syntax seems off
var result = $.map(data.data, function(obj) {
return obj.Label + obj.__text
$(document.body).append(result);
console.log(result);
// $('#site-labels').text(JSON.stringify(tinyJsonFlat)); /// earlier solution
});
}
}
当你分析一个JSON文件,它的行为就像一个正常的Javascript对象,则:
myJSONfile.feed.entry[index].content.properties.ResourceType.element.Label.__text
注意你正在访问的条目的索引:
myJSONfile.feed.entry[index]
const json = {
"feed": {
"id": "[redacted]",
"title": "",
"updated": "2019-01-17T21:01:03Z",
"entry": [
{
"id": "Web/Lists(guid'[redacted]')/Items(1)",
"category": {
"_term": "[redacted]",
"_scheme": "[redacted url]"
},
"link": {
"_rel": "edit",
"_href": "Web/Lists(guid'[redacted]')/Items(1)"
},
"title": "",
"updated": "2019-01-17T21:01:03Z",
"author": {
"name": ""
},
"content": {
"properties": {
"ResourceType": {
"element": {
"Label": {
"__prefix": "d",
"__text": "Guides & Protocols"
},
"TermGuid": {
"__prefix": "d",
"__text": "[redacted]"
},
"WssId": {
"_m:type": "[redacted]",
"__prefix": "d",
"__text": "706"
},
"__prefix": "d"
},
"_m:type": "Collection([redacted])",
"__prefix": "d"
},
"__prefix": "m"
},
"_type": "application/xml"
},
"_m:etag": "\"2\""
}
]
}
}
console.log(json.feed.entry[0].content.properties.ResourceType.element.Label.__text)
// Guides & Protocols
尝试:
Array.prototype.findKey = function(key){
let res = [];
function traverseChildren(input, lookFor) {
//By default, imagine what we are looking at is JSON...
let children = Object.keys(input);
//... however, if it is an array, just stick with that.
if (Array.isArray(input)){
children = input;
}
//Go through everything in the target
for (i = 0; i < children.length; i++) {
//Have we found the *key* we are looking for?
if (children[i] === lookFor && Array.isArray(input)) {
//If so, record the value.
res.push(input[lookFor]);
} else {
//If not, keep searching.
traverseChildren(input[children[i]]);
}
}
}
traverseChildren(this, key);
return res;
}