JSON - 如何访问未命名的属性[从xml转换]

时间:2015-09-09 15:51:13

标签: json xml

什么是JSON等同于:

<attributes>
    <attribute name="ISSO">SOMETHING</attribute>
    <attribute name="ALTISSO">SOMETHING</attribute>
    <attribute name="SSPDate">SOMETHING</attribute>
    <attribute name="DueDate">SOMETHING</attribute>
    <attribute name="Completed">SOMEHING</attribute>
    <attribute name="Notes">SOME NOTES</attribute>
</attributes>

我知道它从

开始
"attributes": { 
    "attribute": [ 
         { "name": "ISSO" , ???? }, 
         {"name": "ALTISSO", ???}, 
         { "name": "SSPDate", ???? } 
    ] 
};

我不理解的是如何访问&#34; SOMETHING&#34; JSON中的部分。我已经尝试过#text和text以及_text和__text。

XML架构是AnyChart

2 个答案:

答案 0 :(得分:1)

http://support.anychart.com/customer/portal/articles/1246584%E2%80%94anychart-6-x-how-to-insert-point-custom-attributes-when-data-is-in-json-format-

上有关于此主题的常见问题解答文章

{'series': 
  {'name': 'Series 1', 
    'point': [
      {'y': 1052, 'name': 'Point A', 
        attributes: {
          attribute: [
          {name: "attr1", custom_attribute_value: "val1"},
          {name: "attr2", custom_attribute_value: "val2"}
        ]}}
    ]
}}

答案 1 :(得分:-1)

您没有提到要访问它的平台或编程语言。但是这里有使用Visual Studio和C#的示例:

首先,我使用此站点从示例XML生成JSON:http://www.utilities-online.info/xmltojson/#.VfBX5xFViko

{
  "attributes": {
    "attribute": [
      {
        "name": "ISSO",
        "text": "SOMETHING"
      },
      {
        "name": "ALTISSO",
        "text": "SOMETHING"
      },
      {
        "name": "SSPDate",
        "text": "SOMETHING"
      },
      {
        "name": "DueDate",
        "text": "SOMETHING"
      },
      {
        "name": "Completed",
        "text": "SOMEHING"
      },
      {
        "name": "Notes",
        "text": "SOME NOTES"
      }
    ]
  }
}

然后,将其粘贴到VS并生成要反序列化的类:

public class Rootobject
{
    public Attributes attributes { get; set; }
}

public class Attributes
{
    public Attribute[] attribute { get; set; }
}

public class Attribute
{
    public string name { get; set; }
    public string text { get; set; }
}

并使用Newtonsoft JSON库反序列化

static void Main(string[] args)
{
    string json = File.ReadAllText("sample.json");
    var attributes = Newtonsoft.Json.JsonConvert.DeserializeObject<Rootobject>(json);
    Console.WriteLine(attributes.attributes.attribute[0].text);
    // Displays SOMETHING
}