0

I have a question on how to deserialize a json array with unfixed name, for instance I have a json string as below.

[
  {
    "37414": "MP",
    "weight": 1000
  },
  {
    "21253": "Develper",
    "weight": 424
  },
  {
    "66344": "APP",
    "weight": 1158
  },
  {
    "1622": "API",
    "weight": 164
  }
]

I also defines a class as below and want to use JSON.NET to deserialize json string to UserTag object. Or should I change the class definition.

public class UserTag
{
    // "37414"
    public long Id { get; set; }

    // MP
    public string Name { get; set; }

    // 424
    public long Weight { get; set; }
}

Thanks in advance!

2 Answers 2

1

Are you using correct json format ?

I think you should use:

[
  {
    "Id" :37414,
    "Name" : "MP",
    "Weight": 1000
  },
  {
    "Id" :21253,
    "Name" : "Develper",
    "Weight": 424
  },
  {
    "Id": 66344,
    "Name" : "APP",
    "Weight": 1158
  }
]

It will deserialize to:

public class UserTag
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Weight { get; set; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Sure, the json format just like my provided. So I confuse on it.
0
var result = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(json);

foreach (var item in result)
{
    foreach (var kv in item)
    {
        Console.WriteLine(kv.Key + ": " + kv.Value);
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.