将json字符串解析为C#对象 [英] Parse json string to C# objects
本文介绍了将json字符串解析为C#对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
字符串a =[[\test,test \,\{\\ \ prop1\\\:1,\\\ prop2\\\:\\\ test\\\,\\ \prop3 \\\:\\\test \\\} \],[\test2,test2 \,\{\ \\ prop1\\\:2,\\\ prop2\\\:\\\ test2\\\,\ \\prop3 \\\:\\\test2 \\\} \]];
并尝试将此字符串解析为对象。试过,比如,一切:(没什么用。我不太确定,如果我正确理解这个json结构。我真的很感激,如果有人可以帮我这个。这里是格式化的JSON:
[
[
test,
{
prop1:1,
prop2:test,
prop3:test
}
],
[
test2,
{
prop1:2,
prop2:test2,
prop3:test2
}
]
]
但是我仍然不明白它的结构。
我尝试过:
我为这个json创建了一些对象,据我所知,它的结构如下:
class Details
{
public int prop1 {get; set;}
public string prop2 {get; set;}
public string prop3 {get; set;}
}
class Name
{
public string FullName {get; set;}
}
class Class
{
public Name FullName {get;组; }
public Details Details {get;组; }
}
尝试以这种方式反序列化:
string a =[[\ test,test \,\{\\\prop1 \\\:1,\\\prop2 \\\:\\ \ test\\\,\\\ prop3\\\:\\\ test\\\} \],[ \test2,test2 \,\{\\\prop1 \\\:2,\\\prop2 \\\:\ \\ test2\\\,\\\ prop3\\\:\\\ test2\\\} \] ];
列表<课程> obj = JsonConvert.DeserializeObject< List< Class>>(a);
解决方案
好的,找到了办法:
JArray数组= JArray.Parse(a);
foreach(数组中的JToken jToken)
{
JToken [] innerArray = jToken.ToArray();
string fullName = innerArray [0] .Value< string>();
详细信息details = JObject.Parse(innerArray [1] .Value< string>())。ToObject< Details>();
}
这篇文章将向您展示一种更简单的方法:在C#中使用JSON& VB [ ^ ]
Hi there! I am dealing with a non standard json string, here:
string a = "[[\"test, test\",\"{\\\"prop1\\\":1,\\\"prop2\\\":\\\"test\\\",\\\"prop3\\\":\\\"test\\\"}\"],[\"test2, test2\",\"{\\\"prop1\\\":2,\\\"prop2\\\":\\\"test2\\\",\\\"prop3\\\":\\\"test2\\\"}\"]]";
And trying to parse this string to an object. Tried, like, everything :( Nothing works. I am not really sure, if I understand this json structure correctly. I would really appreciate, if someone could help me with this. Here is formatted JSON:
[ [ "test", { "prop1": 1, "prop2": "test", "prop3": "test" } ], [ "test2", { "prop1": 2, "prop2": "test2", "prop3": "test2" } ] ]
But I still do not understand its structure.
What I have tried:
I have created some objects for this json, as far as I can understand its structure. Here they are:
class Details { public int prop1 { get; set; } public string prop2 { get; set; } public string prop3 { get; set; } } class Name { public string FullName { get; set; } } class Class { public Name FullName { get; set; } public Details Details { get; set; } }
And trying to deserialize this way:
string a = "[[\"test, test\",\"{\\\"prop1\\\":1,\\\"prop2\\\":\\\"test\\\",\\\"prop3\\\":\\\"test\\\"}\"],[\"test2, test2\",\"{\\\"prop1\\\":2,\\\"prop2\\\":\\\"test2\\\",\\\"prop3\\\":\\\"test2\\\"}\"]]"; List< Class > obj = JsonConvert.DeserializeObject<List<Class>>(a);
解决方案
Alright, found a way:
JArray array = JArray.Parse(a); foreach(JToken jToken in array) { JToken[] innerArray = jToken.ToArray(); string fullName = innerArray[0].Value<string>(); Details details = JObject.Parse(innerArray[1].Value<string>()).ToObject<Details>(); }
THis article will show you an easier way: Working with JSON in C# & VB[^]
这篇关于将json字符串解析为C#对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文