使用Newtonsoft JSON反序列化包含数组/列表的对象,使用字典。

huangapple go评论82阅读模式
英文:

using newtonsoft json deserialize object that contains array/list using dictionary

问题

我想将 JSON 转换为对象。下面的代码有效。

  1. internal class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. string json = @"{
  6. 'name': 'Ayrton Senna',
  7. 'friends': [
  8. {
  9. 'name': 'John',
  10. 'age': 34
  11. },
  12. {
  13. 'name': 'Jack',
  14. 'age': 32
  15. }
  16. ]
  17. }";
  18. var person = JsonConvert.DeserializeObject<Person>(json);
  19. }
  20. public class Person
  21. {
  22. public string Name { get; set; }
  23. public List<Friend> Friends { get; set; }
  24. public class Friend
  25. {
  26. public string Name { get; set; }
  27. public int Age { get; set; }
  28. }
  29. }
  30. }

然而,在 Person 类中,我希望属性 List<Friend> 变成字典。键将是字符串,应该从 JSON 中获取而不是硬编码。值将是对象列表。

示例:

  1. public class Person
  2. {
  3. public string Name { get; set; }
  4. public Dictionary<string, List<Friend>> Friends { get; set; }
  5. public class Friend
  6. {
  7. public string Name { get; set; }
  8. public int Age { get; set; }
  9. }
  10. }

我之所以这样做是因为当我有不同的 JSON 时,例如:

  1. {
  2. "id": 1,
  3. "name": "Ayrton Senna",
  4. "mates": [
  5. {
  6. "name": "John",
  7. "age": 12
  8. },
  9. {
  10. "name": "Jack",
  11. "age": 32
  12. }
  13. ]
  14. }

另一个示例的 JSON:

  1. {
  2. "id": 1,
  3. "name": "Ayrton Senna",
  4. "companions": [
  5. {
  6. "name": "John",
  7. "age": 12
  8. },
  9. {
  10. "name": "Jack",
  11. "age": 32
  12. }
  13. ]
  14. }

我需要捕获/存储列表的名称,如 "friends"、"mates"、"companions",因为我以后需要显示它们。我将有 20 个类似的 JSON 文件,唯一不同的是这个属性的名称。所以我需要从 JSON 中读取它并以某种方式存储它。

我想要一个更 "通用" 的类。这是否可行?如果您有其他建议,请告诉我。将不胜感激。

英文:

I want to convert JSON to object. The code below works fine.

  1. internal class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. string json = @&quot;{
  6. &#39;name&#39;: &#39;Ayrton Senna&#39;,
  7. &#39;friends&#39;: [
  8. {
  9. &#39;name&#39;: &#39;John&#39;,
  10. &#39;age&#39;: 34
  11. },
  12. {
  13. &#39;name&#39;: &#39;Jack&#39;,
  14. &#39;age&#39;: 32
  15. }
  16. ]
  17. }&quot;;
  18. var person = JsonConvert.DeserializeObject&lt;Person&gt;(json);
  19. }
  20. public class Person
  21. {
  22. public string Name { get; set; }
  23. public List&lt;Friend&gt; Friends { get; set; }
  24. public class Friend
  25. {
  26. public string Name { get; set; }
  27. public int Age { get; set; }
  28. }
  29. }
  30. }

However in the Person Class I want the property List<Friend> to be dictionary.
The key will be string that should be "friends" (somehow taken from the Json and not hardcoded).
The value will be list of objects.

Example:

  1. public class Person
  2. {
  3. public string Name { get; set; }
  4. public Dictionary&lt;string, List&lt;Friend&gt;&gt; Friends { get; set; }
  5. public class Friend
  6. {
  7. public string Name { get; set; }
  8. public int Age { get; set; }
  9. }
  10. }

The reason I want this is so when i will have different JSON for example:

  1. {
  2. &quot;id&quot;: 1,
  3. &quot;name&quot;: &quot;Ayrton Senna&quot;,
  4. &quot;mates&quot;: [
  5. {
  6. &quot;name&quot;: &quot;John&quot;,
  7. &quot;age&quot;: 12
  8. },
  9. {
  10. &quot;name&quot;: &quot;Jack&quot;,
  11. &quot;age&quot;: 32
  12. }
  13. ]
  14. }

Another example Json:

  1. {
  2. &quot;id&quot;: 1,
  3. &quot;name&quot;: &quot;Ayrton Senna&quot;,
  4. &quot;companions&quot;: [
  5. {
  6. &quot;name&quot;: &quot;John&quot;,
  7. &quot;age&quot;: 12
  8. },
  9. {
  10. &quot;name&quot;: &quot;Jack&quot;,
  11. &quot;age&quot;: 32
  12. }
  13. ]
  14. }

I need to capture/store the name of the list like: "friends", "mates", "companions" because i will need to display them later. I will have 20 Json files like this and only this property name will be different. So i need to read ir from the json and store it somehow.

I want a class that will be more "generic".

Is this even doable? If you have some other suggestions please do tell. Any help will be much appreciated!

答案1

得分: 1

如果唯一的区别是数组的名称,您可以继续使用您已经有的类,并使用以下代码:

  1. var jObj = JObject.Parse(json);
  2. var friends = jObj.Properties()
  3. .Where(p => p.Value.Type == JTokenType.Array)
  4. .SingleOrDefault();
  5. Person person = jObj.ToObject<Person>();
  6. person.FriendsKind = friends.Name;
  7. person.Friends = friends.Value.Select(p => p.ToObject<Friend>()).ToList();

公共类 Person 如下:

  1. public class Person
  2. {
  3. public int Id { get; set; }
  4. public string Name { get; set; }
  5. public string FriendsKind { get; set; }
  6. public List<Friend> Friends { get; set; }
  7. }
英文:

if the only difference is the array name, you can leave the class you have already and use this code

  1. var jObj = JObject.Parse(json);
  2. var friends = jObj.Properties()
  3. .Where(p =&gt; p.Value.Type == JTokenType.Array)
  4. .SingleOrDefault();
  5. Person person = jObj.ToObject&lt;Person&gt;();
  6. person.FriendsKind= friends.Name;
  7. person.Friends = friends.Value.Select(p =&gt; p.ToObject&lt;Friend&gt;()).ToList();
  8. public class Person
  9. {
  10. public int Id { get; set; }
  11. public string Name { get; set; }
  12. public string FriendsKind {get; set;}
  13. public List&lt;Friend&gt; Friends { get; set; }
  14. }

答案2

得分: 1

以下是您要翻译的代码部分:

  1. var jObj = JObject.Parse(json);
  2. var friends = jObj.Properties().ToArray()[1];
  3. var person = new Person
  4. {
  5. Name = jObj.GetValue("name").Value<string>(),
  6. Friends = new Dictionary<string, List<Person.Friend>>
  7. {
  8. {
  9. friends.Name,
  10. friends.Value.Select(p => p.ToObject<Person.Friend>()).ToList()
  11. }
  12. }
  13. };
  14. public class Person
  15. {
  16. public string Name { get; set; }
  17. public Dictionary<string, List<Friend>> Friends { get; set; }
  18. public class Friend
  19. {
  20. public string Name { get; set; }
  21. public int Age { get; set; }
  22. }
  23. }
英文:
  1. var jObj = JObject.Parse(json);
  2. var friends = jObj.Properties().ToArray()[1];
  3. var person = new Person
  4. {
  5. Name = jObj.GetValue(&quot;name&quot;).Value&lt;string&gt;(),
  6. Friends = new Dictionary&lt;string, List&lt;Person.Friend&gt;&gt;
  7. {
  8. {
  9. friends.Name,
  10. friends.Value.Select(p =&gt; p.ToObject&lt;Person.Friend&gt;()).ToList()
  11. }
  12. }
  13. };
  14. public class Person
  15. {
  16. public string Name { get; set; }
  17. public Dictionary&lt;string, List&lt;Friend&gt;&gt; Friends { get; set; }
  18. public class Friend
  19. {
  20. public string Name { get; set; }
  21. public int Age { get; set; }
  22. }
  23. }

huangapple
  • 本文由 发表于 2023年2月14日 04:22:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440809.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定