简体   繁体   中英

How to deserialize json without key value pair? C#

I get the following JSON data from JSON endpoint API's which I can't change:

[
  [
    "NameA",
    [
      "AAA"
    ]
  ],
  [
    "NameB",
    [
      "BBB"
    ]
  ],
  [
    "NameC",
    [
      "CCC"
    ]
  ]
]

I know its a valid JSON. Though I am not able to parse it in C#. I tried to generate the class for this JSON using online tools but it did not help. Any help would be appreciated as I'm really stuck on this.

You can use MessagePack via Nuget .

CLASS OBJECTS

[MessagePackObject]
public class Item1
{
    [Key(0)]
    public string Key { get; set; }
    [Key(1)]
    public Item2 Value { get; set; }
}

[MessagePackObject]
public class Item2
{
    [Key(0)]
    public string Value { get; set; }
}

SERIALIZATION

var json = File.ReadAllText("json1.json");
var byteArray = MessagePackSerializer.ConvertFromJson(json);
var itemList = MessagePackSerializer.Deserialize<List<Item1>>(byteArray);

OUTPUT
在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM