簡體   English   中英

解析嵌套 JSON Object

[英]Parsing a Nested JSON Object

我需要遞歸解析 JSON object。

這是一個示例 JSON:

const obj = {
  tag: 'AA',
  type: 'constructed',
  value: 'ABCD1',
  child: [
    {
      tag: 'BB',
      type: 'constructed',
      value: 'ABCD2',      
      child: [
        {
          tag: 'CC',
          type: 'constructed',
          value: 'ABCD3',          
          child: [
            {
              tag: 'DD',
              type: 'primitive',
              value: 'ABCD4',
              child: []
            },
            {
              tag: 'EE',
              type: 'constructed',
              value: 'ABCD5',
              child: [
                {
                  tag: 'FF',
                  type: 'primitive',
                  value: 'ABCD6',
                  child: []
                },
                {
                  tag: 'GG',
                  type: 'primitive',
                  value: 'ABCD7',
                  child: []
                }                
              ]
            },
            {
              tag: 'HH',
              type: 'primitive',
              value: 'ABCD8',
              child: []
            }  
          ]
        }
      ]
    },
    {
      tag: 'II',
      type: 'primitive',
      value: 'ABCD9',
      child: []
    }  
  ]
}

而 output 應該是這樣的:

{
  "AA": [
    {
      "BB": [
        {
          "CC": [
            {
              "DD": "ABCD4"
            },
            {
              "EE": [
                {
                  "FF": "ABCD6"
                },
                {
                  "GG": "ABCD7"
                }
              ]
            },
            {
              "HH": "ABCD8"
            }
          ]
        }
      ]
    },
    {
      "II": "ABCD9"
    }
  ]
}

基本上, constructed類型的應該有一個嵌套的 object,而primitive類型的應該直接有一個鍵值對。 object 可以 go 非常深,並且可以在同一級別上同時具有構造對象和原始對象(如我的示例)。

這是我當前的代碼:

let jsonOutput = {}
parseData(obj, jsonOutput)

function parseData(jsonToParse, jsonOutput) {
  const type = jsonToParse.type
  const tag = jsonToParse.tag
  const value = jsonToParse.value
  let prev = jsonOutput
  
  if (type === 'constructed') {
    prev[tag] = []
    return parseData(jsonToParse.child[0], prev[tag])
  } else if (type === 'primitive') {
    prev[tag] = value
    return parseData(jsonToParse.child, prev[tag])    
  }
}

這是我的小提琴: https://jsfiddle.net/kzaiwo/0v6a2tp8/16/

但我不能讓它遍歷整個 object。遞歸並不是我最擅長的領域,但我認為這是實現它的最佳方式。我錯過了什么? 請幫助!

謝謝!

您可以使用以下遞歸reduceObj(obj) function,簡短而貼心:

 const obj = { tag: 'AA', type: 'constructed', value: 'ABCD1', child: [ { tag: 'BB', type: 'constructed', value: 'ABCD2', child: [ { tag: 'CC', type: 'constructed', value: 'ABCD3', child: [ { tag: 'DD', type: 'primitive', value: 'ABCD4', child: [] }, { tag: 'EE', type: 'constructed', value: 'ABCD5', child: [ { tag: 'FF', type: 'primitive', value: 'ABCD6', child: [] }, { tag: 'GG', type: 'primitive', value: 'ABCD7', child: [] } ] }, { tag: 'HH', type: 'primitive', value: 'ABCD8', child: [] } ] } ] }, { tag: 'II', type: 'primitive', value: 'ABCD9', child: [] } ] } function reduceObj(obj) { return { [obj.tag]: obj.type === 'primitive'? obj.value: obj.child.map(reduceObj) }; } const result = reduceObj(obj); console.log(result);

Function ES6 之前的語法:

    function reduceObj(obj) {
      let o = {};
      o[obj.tag] = obj.child.map(function(childObj) {
        return obj.type === 'primitive'
          ? obj.value
          : reduceObj(childObj);
      });
      return o;
    }

希望這可以幫助你,

 const obj = { tag: 'AA', type: 'constructed', value: 'ABCD1', child: [ { tag: 'BB', type: 'constructed', value: 'ABCD2', child: [ { tag: 'CC', type: 'constructed', value: 'ABCD3', child: [ { tag: 'DD', type: 'primitive', value: 'ABCD4', child: [] }, { tag: 'EE', type: 'constructed', value: 'ABCD5', child: [ { tag: 'FF', type: 'primitive', value: 'ABCD6', child: [] }, { tag: 'GG', type: 'primitive', value: 'ABCD7', child: [] } ] }, { tag: 'HH', type: 'primitive', value: 'ABCD8', child: [] } ] } ] }, { tag: 'II', type: 'primitive', value: 'ABCD9', child: [] } ] } function parseData(obj) { const result = {}; if (obj.type === 'primitive') { result[obj.tag] = obj.value; } else if (obj.type === 'constructed') { result[obj.tag] = obj.child.map(parseData); } return result; } const parsedJson = parseData(obj); console.log(parsedJson);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM