技术问询:如何将JSON中的方括号[]转换为XML属性?
Got it, let's break this down based on the pattern you already use for JSON objects!
First, to recap your existing approach: when dealing with nested JSON objects (the {} parts), you use ["key"] to chain nested properties—like turning "site": { "name": "Main" } into site["name"] for the AttributeName.
For JSON arrays (the [] sections), you just need to add an 0-based index reference in square brackets to target specific elements in the array, then follow it with the same ["key"] syntax you already know for object properties.
Let’s apply this to your example JSON:
"communicationItems": [ { "id": 123456, "type": "" }, { "id": 1, "name": "Email" }, { "value": "test@test.com" } ]
Here’s how the name and value properties map to your XML format:
- For the
nameproperty in the second array element (index 1):<DataColumn Value="Email" AttributeName="communicationItems[1]["name"]"/> - For the
valueproperty in the third array element (index 2):<DataColumn Value="test@test.com" AttributeName="communicationItems[2]["value"]"/>
To make the pattern even clearer, here are a couple more examples from your JSON:
- First element’s
id:<DataColumn Value="123456" AttributeName="communicationItems[0]["id"]"/> - Second element’s
id:<DataColumn Value="1" AttributeName="communicationItems[1]["id"]"/>
The core rule to remember:
- Start with the top-level array key (
communicationItems) - Add
[index]to specify which array element you’re targeting (count starts at 0) - Append
["property-name"]to point to the property inside that element, just like you do for JSON objects.
内容的提问来源于stack exchange,提问作者Aaron Cohee

