英文:
using newtonsoft json deserialize object that contains array/list using dictionary
问题
我想将 JSON 转换为对象。下面的代码有效。
internal class Program
{
static void Main(string[] args)
{
string json = @"{
'name': 'Ayrton Senna',
'friends': [
{
'name': 'John',
'age': 34
},
{
'name': 'Jack',
'age': 32
}
]
}";
var person = JsonConvert.DeserializeObject<Person>(json);
}
public class Person
{
public string Name { get; set; }
public List<Friend> Friends { get; set; }
public class Friend
{
public string Name { get; set; }
public int Age { get; set; }
}
}
}
然而,在 Person 类中,我希望属性 List<Friend> 变成字典。键将是字符串,应该从 JSON 中获取而不是硬编码。值将是对象列表。
示例:
public class Person
{
public string Name { get; set; }
public Dictionary<string, List<Friend>> Friends { get; set; }
public class Friend
{
public string Name { get; set; }
public int Age { get; set; }
}
}
我之所以这样做是因为当我有不同的 JSON 时,例如:
{
"id": 1,
"name": "Ayrton Senna",
"mates": [
{
"name": "John",
"age": 12
},
{
"name": "Jack",
"age": 32
}
]
}
另一个示例的 JSON:
{
"id": 1,
"name": "Ayrton Senna",
"companions": [
{
"name": "John",
"age": 12
},
{
"name": "Jack",
"age": 32
}
]
}
我需要捕获/存储列表的名称,如 "friends"、"mates"、"companions",因为我以后需要显示它们。我将有 20 个类似的 JSON 文件,唯一不同的是这个属性的名称。所以我需要从 JSON 中读取它并以某种方式存储它。
我想要一个更 "通用" 的类。这是否可行?如果您有其他建议,请告诉我。将不胜感激。
英文:
I want to convert JSON to object. The code below works fine.
internal class Program
{
static void Main(string[] args)
{
string json = @"{
'name': 'Ayrton Senna',
'friends': [
{
'name': 'John',
'age': 34
},
{
'name': 'Jack',
'age': 32
}
]
}";
var person = JsonConvert.DeserializeObject<Person>(json);
}
public class Person
{
public string Name { get; set; }
public List<Friend> Friends { get; set; }
public class Friend
{
public string Name { get; set; }
public int Age { get; set; }
}
}
}
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:
public class Person
{
public string Name { get; set; }
public Dictionary<string, List<Friend>> Friends { get; set; }
public class Friend
{
public string Name { get; set; }
public int Age { get; set; }
}
}
The reason I want this is so when i will have different JSON for example:
{
"id": 1,
"name": "Ayrton Senna",
"mates": [
{
"name": "John",
"age": 12
},
{
"name": "Jack",
"age": 32
}
]
}
Another example Json:
{
"id": 1,
"name": "Ayrton Senna",
"companions": [
{
"name": "John",
"age": 12
},
{
"name": "Jack",
"age": 32
}
]
}
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
如果唯一的区别是数组的名称,您可以继续使用您已经有的类,并使用以下代码:
var jObj = JObject.Parse(json);
var friends = jObj.Properties()
.Where(p => p.Value.Type == JTokenType.Array)
.SingleOrDefault();
Person person = jObj.ToObject<Person>();
person.FriendsKind = friends.Name;
person.Friends = friends.Value.Select(p => p.ToObject<Friend>()).ToList();
公共类 Person
如下:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public string FriendsKind { get; set; }
public List<Friend> Friends { get; set; }
}
英文:
if the only difference is the array name, you can leave the class you have already and use this code
var jObj = JObject.Parse(json);
var friends = jObj.Properties()
.Where(p => p.Value.Type == JTokenType.Array)
.SingleOrDefault();
Person person = jObj.ToObject<Person>();
person.FriendsKind= friends.Name;
person.Friends = friends.Value.Select(p => p.ToObject<Friend>()).ToList();
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public string FriendsKind {get; set;}
public List<Friend> Friends { get; set; }
}
答案2
得分: 1
以下是您要翻译的代码部分:
var jObj = JObject.Parse(json);
var friends = jObj.Properties().ToArray()[1];
var person = new Person
{
Name = jObj.GetValue("name").Value<string>(),
Friends = new Dictionary<string, List<Person.Friend>>
{
{
friends.Name,
friends.Value.Select(p => p.ToObject<Person.Friend>()).ToList()
}
}
};
public class Person
{
public string Name { get; set; }
public Dictionary<string, List<Friend>> Friends { get; set; }
public class Friend
{
public string Name { get; set; }
public int Age { get; set; }
}
}
英文:
var jObj = JObject.Parse(json);
var friends = jObj.Properties().ToArray()[1];
var person = new Person
{
Name = jObj.GetValue("name").Value<string>(),
Friends = new Dictionary<string, List<Person.Friend>>
{
{
friends.Name,
friends.Value.Select(p => p.ToObject<Person.Friend>()).ToList()
}
}
};
public class Person
{
public string Name { get; set; }
public Dictionary<string, List<Friend>> Friends { get; set; }
public class Friend
{
public string Name { get; set; }
public int Age { get; set; }
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论