[英]Deserialize json string into a list of .NET objects
Requirement要求
I am trying to build a function that takes a json string as input.我正在尝试构建一个以 json 字符串作为输入的函数。 and outputs list of object.
并输出对象列表。 The json string is in a similar format to this:
json 字符串的格式与此类似:
{\"custlist\":[{\"cust_name\":\"Vincent\",\"cust_id\":\"klq:206f387:2d08m92t6\"},{\"cust_name\":\"Joyce\",\"cust_id\":\"125g:1474grx:2d03t9dld\"}]}
My Search我的搜索
There are plenty of solutions deserialize json array to list of objects, but the array starts at the beginning of the string.有很多解决方案将 json 数组反序列化为对象列表,但数组从字符串的开头开始。 ie without the \\"custlist\\": part
即没有 \\"custlist\\": 部分
If we have \\"custlist\\": part in the json string, those solutions break.如果我们在 json 字符串中有 \\"custlist\\": 部分,那么这些解决方案就会中断。
My Code我的代码
Here is my code in C#.这是我在 C# 中的代码。 It is working, but I had to use regular expression to match the input string.
它正在工作,但我必须使用正则表达式来匹配输入字符串。 Seems over-complicated.
看起来过于复杂了。 There must be an easier way.
必须有更简单的方法。 Anyone knows, please kindly advise
谁知道,请指教
public void Test()
{
string str = {\"custlist\":[{\"cust_name\":\"Vincent\",\"cust_id\":\"klq:206f387:2d08m92t6\"},{\"cust_name\":\"Joyce\",\"cust_id\":\"125g:1474grx:2d03t9dld\"}]};
List<Customer> list = Json2List<Customer>(str);
foreach (Customer c in list)
{
console.writeline ("name=" + c.cust_name);
console.writeline ("id=" + c.cust_id);
}
}
public List<T> Json2List<T>(string s)
{
string json_listname = Regex.Match(s, "\"" + @"(\w+?)" + "\":").Groups[0].Value;
JObject jObject = JObject.Parse(s);
List<JToken> jTokenList = jObject.GetValue(json_listname).ToList();
List<T> LstT = new List<T>();
foreach (JToken jt in jTokenList)
{
T obj = jt.ToObject<T>();
LstT.Add(obj);
}
return LstT;
}
public class Customer
{
public string cust_name { get; set; }
public string cust_id { get; set; }
}
I am really lost as to what the problem is, but essentially:我真的不知道问题是什么,但本质上:
public class CustomerList {
[JsonProperty("custlist")]
public Customer[] Customers { get; set; }
}
public class Customer
{
[JsonProperty("cust_name")]
public string Name { get; set; }
[JsonProperty("cust_id")]
public string Id { get; set; }
}
var sample = "{\"custlist\":[{\"cust_name\":\"Vincent\"},{\"cust_id\":\"klq206f3872d08m92t6\"},{\"cust_name\":\"Joyce\"},{\"cust_id\":\"125g1474grx2d03t9dld\"}]}";
var result = JsonConvert.DeserializeObject<CustomerList>(sample).Customers;
// Or!
var dictResult = JsonConvert.DeserializeObject<Dictionary<string, Customer[]>>(sample)["custlist"];
Looks like it's a JSON object that is stored in a JSON string.看起来它是一个存储在 JSON 字符串中的 JSON 对象。
So deserialize it as a string first, then as a list of the correct type.因此,首先将其反序列化为字符串,然后将其反序列化为正确类型的列表。 You can do both using
JsonConvert.DeserializeObject
:您可以同时使用
JsonConvert.DeserializeObject
:
Update: I just realized that the array in the JSON is a property of the JSON object, which I didn't account for here.更新:我刚刚意识到 JSON 中的数组是 JSON 对象的一个属性,这里我没有考虑。 I don't have time to fix that right now, but I hope you get the idea.
我现在没有时间解决这个问题,但我希望你能明白。
public List<T> Json2List<T>(string s)
{
string json_object = JsonConvert.DeserializeObject<string>(s);
return JsonConvert.DeserializeObject<List<T>>(json_object);
}
But I would also look into why the data you're getting is double-serialized like that.但我也会研究为什么你得到的数据是这样双重序列化的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.