最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

从大量的嵌套外部文件选择JSON节点

运维笔记admin8浏览0评论

从大量的嵌套外部文件选择JSON节点

从大量的嵌套外部文件选择JSON节点

我有一个本地JSON文件的工作,并尝试从它使某些节点。为什么我问它在计算器上的原因,然而,就是在JSON文件本身在很大程度上嵌套一吨的子节点,我不知道如何遍历像这样的一个JSON文件。我扁平的数据,以试图缓解这个问题,但它似乎没有做多好。

我想专门访问__text(即__text": "Guides & Protocols"

Here's a sample of the data:

{
    "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\""
            }
         ...
     ]
 }

JavaScript:

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
发布评论

评论列表(0)

  1. 暂无评论