javascript - 如何处理这个 json 结构

标签 javascript json

我使用一些在线链接将XML格式的数据转换为JSON,我得到的结果是

   {
  "SOAP-ENV:Envelope": {
    "-xmlns:SOAP-ENV": "http://schemas.xmlsoap.org/soap/envelope/",
    "-xmlns:SOAP-ENC": "http://schemas.xmlsoap.org/soap/encoding/",
    "-xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
    "-xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
    "-xmlns:hdr": "urn:PASAHeader",
    "-xmlns:ns": "urn:PSFVUC_DMTR",
    "-xmlns:ex": "urn:PASAexploitationWS",
    "SOAP-ENV:Body": {
      "-SOAP-ENV:encodingStyle": "http://schemas.xmlsoap.org/soap/encoding/",
      "-id": "_0",
      "ns:getEleLocTopicDtlResp": {
        "tControle": {
          "-xsi:type": "ns:tControle",
          "iErrorType": {
            "-xsi:type": "xsd:int",
            "#text": "0"
          },
          "iRetError": {
            "-xsi:type": "xsd:int",
            "#text": "0"
          },
          "sErrorMessage": { "-xsi:type": "xsd:string" }
        },
        "tGlobalSetting": {
          "-xsi:type": "ns:tGlobalSettingTopic",
          "iGlobalSettingId": {
            "-xsi:type": "xsd:int",
            "#text": "10"
          },
          "sGlobalSettingCode": {
            "-xsi:type": "xsd:string",
            "#text": "RAJA"
          },
          "iDisplayMode": {
            "-xsi:type": "xsd:int",
            "#text": "0"
          },
          "iOrderVariant": {
            "-xsi:type": "xsd:int",
            "#text": "0"
          }
        },
        "pListTopic": {
          "-xsi:type": "ns:sListELTopic",
          "nTopic": {
            "-xsi:type": "xsd:int",
            "#text": "2"
          },
          "pTListTopic": {
            "-xsi:type": "SOAP-ENC:Array",
            "-SOAP-ENC:offset": "[0]",
            "-SOAP-ENC:arrayType": "ns:tELTopic[2]",
            "item": [
              {
                "-xsi:type": "ns:tELTopic",
                "iTopicId": {
                  "-xsi:type": "xsd:int",
                  "#text": "5"
                },
                "sTopicName": {
                  "-xsi:type": "xsd:string",
                  "#text": "TOP1 TESTING PLEASEDONTDELETE"
                },
                "pListClearCriteria": {
                  "-xsi:type": "ns:sListClearCriteria",
                  "nClearCriteria": {
                    "-xsi:type": "xsd:int",
                    "#text": "0"
                  }
                },
                "pListImage": {
                  "-xsi:type": "ns:sListImage",
                  "nImage": {
                    "-xsi:type": "xsd:int",
                    "#text": "0"
                  }
                },
                "pListTag": {
                  "-xsi:type": "ns:sListELTag",
                  "nTag": {
                    "-xsi:type": "xsd:int",
                    "#text": "0"
                  }
                }
              },
              {
                "-xsi:type": "ns:tELTopic",
                "-SOAP-ENC:position": "[1]",
                "iTopicId": {
                  "-xsi:type": "xsd:int",
                  "#text": "6"
                },
                "sTopicName": {
                  "-xsi:type": "xsd:string",
                  "#text": "TOP2 TESTING PLEASEDONTDELETE"
                },
                "pListClearCriteria": {
                  "-xsi:type": "ns:sListClearCriteria",
                  "nClearCriteria": {
                    "-xsi:type": "xsd:int",
                    "#text": "0"
                  }
                },
                "pListImage": {
                  "-xsi:type": "ns:sListImage",
                  "nImage": {
                    "-xsi:type": "xsd:int",
                    "#text": "0"
                  }
                },
                "pListTag": {
                  "-xsi:type": "ns:sListELTag",
                  "nTag": {
                    "-xsi:type": "xsd:int",
                    "#text": "0"
                  }
                }
              }
            ]
          }
        }
      }
    }
  }
}

现在我想在JS中动态添加和附加值,但我从未遇到过如此复杂的JSON结构,我遇到过带有键值对的JSON。

要求将此 JSON(更改后)转换为 XML 并发送到服务器。 我们如何处理这样的 JSON 结构。使用这样的 JSON 结构是否存在任何问题。 使用我要使用的确切 json 进行更新。

场景:假设我必须附加“pListTopic”项目,如何管理“-xsi:type”:“SOAP-ENC:Array”

对于每个项目,我的目标是动态地向数组添加一个新元素,但我仍然必须维护每个元素的“-xsi:type”:值,我怎样才能实现这一点。

最佳答案

这是正常的 JSON。您可以按照通常的方式附加和修改值。例如这段代码

var test={"first":{"first1":"blabla","first2":"blabla2"},"second":"blabla3","third":{"third1":"blabla4","third2":"blabla5"}}
console.log(test)
console.log('-----')
console.log(test.first)
console.log('-----')
console.log(test.first.first1)
console.log('-----')
test.fourth = "just added";
test.third.third2 = "just corrected";
test.third.third3 = "hey it works";
console.log(test)

在屏幕上打印(使用nodejs)以下内容:

{ first: { first1: 'blabla', first2: 'blabla2' },
  second: 'blabla3',
  third: { third1: 'blabla4', third2: 'blabla5' } }
-----
{ first1: 'blabla', first2: 'blabla2' }
-----
blabla
-----
{ first: { first1: 'blabla', first2: 'blabla2' },
  second: 'blabla3',
  third: 
   { third1: 'blabla4',
     third2: 'just corrected',
     third3: 'hey it works' },
  fourth: 'just added' }

这回答了你的问题。然而,将该对象转换为 JSON 并仅对其进行修改,然后将其更改为 XML 并没有多大意义。我会修改原来的 XML 结构。

关于javascript - 如何处理这个 json 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30749602/

相关文章:

c# - PayPal Rest API - 更新计费计划返回 URL

javascript - jQuery 显示和隐藏表格 td 中的元素

javascript - Javascript 中 for 循环内的闭包结果令人困惑

javascript - 根据单元格的值显示/隐藏表格行

javascript - 是否有推荐的通用模式来内存 ajax 调用?

javascript - 将多个值和名称添加到 CSS 样式属性

java - Gson 将新对象数组附加到现有 JSON 文件

android - Retrofit 2.0 转换器返回 NULL

json - 检查 fetch_json sub 中的 HTTP 代码/保存以前的输出以在 Perl 中进行备份

c# - 为什么 Json.NET 试图反序列化我的 get only 属性?