[英]C# Deserialize string of multiple arrays with json-string in it explicit to List<string>
i have a json-string, or bit more something like a "string of arrays": 我有一个json-string,或更像“数组字符串”:
"[
{
"type":"radio-group",
"label":"Radio-Button-Gruppe",
"name":"radio-group-1556028993486",
"className":"iCheck",
"values":[
{
"label":"aaaaaaa",
"value":"aaaaaaa"
},
{
"label":"bbbbbbbbb",
"value":"bbbbbbbbb"
},
{
"label":"cccccccccccc",
"value":"cccccccccccc"
}
]
}
],
[
...
],
[
{
"type":"header",
"label":"Überschrift"
}
]"
Now I want to have a List<string>
of each array in this string. 现在,我想在此字符串中包含每个数组的
List<string>
。 Something like: 就像是:
List<string> x[0] = "{
"type":"radio-group",
"label":"Radio-Button-Gruppe",
"name":"radio-group-1556028993486",
"className":"iCheck",
"values":[
{
"label":"aaaaaaa",
"value":"aaaaaaa"
},
{
"label":"bbbbbbbbb",
"value":"bbbbbbbbb"
},
{
"label":"cccccccccccc",
"value":"cccccccccccc"
}
]
}"
What's the best way to do that? 最好的方法是什么?
I already tried out JsonConvert.DeserializeObject<IEnumerable<string>>()
but my problem is that he wants to deserialize my jsons to an object. 我已经尝试过
JsonConvert.DeserializeObject<IEnumerable<string>>()
但是我的问题是他想将我的json反序列化为一个对象。 But I want to keep them as a string und put them to my list. 但我想将它们保留为字符串,并将它们放入我的列表。
Why I need a list of string for each array? 为什么我需要每个数组的字符串列表? Because I use the json-strings inside the arrays to render a form.
因为我在数组中使用json-strings来呈现表单。 each array show you a page of the form, and the json is the data for rendering this form.
每个数组都会显示表单的页面,而json是用于呈现此表单的数据。 to render this form i have to go through a loop through all of this arrays and render the json in it for every page.
为了呈现这种形式,我必须遍历所有这些数组并为每个页面呈现其中的json。
You can use JsonConvert.DeserializeObject<IEnumerable<JToken>>(json)
where json
is each of your top level arrays. 您可以使用
JsonConvert.DeserializeObject<IEnumerable<JToken>>(json)
,其中json
是每个顶级数组。 Then you can iterate through the results and use .ToString()
on each JToken object. 然后,您可以遍历结果并在每个JToken对象上使用
.ToString()
。
As others have pointed out, you don't have valid JSON, so I didn't try to give a solution to parse out the top level arrays. 正如其他人指出的那样,您没有有效的JSON,因此我没有尝试给出解析顶级数组的解决方案。
var json = @"[
{
""type"":""radio-group"",
""label"":""Radio-Button-Gruppe"",
""name"":""radio-group-1556028993486"",
""className"":""iCheck"",
""values"":[
{
""label"":""aaaaaaa"",
""value"":""aaaaaaa""
},
{
""label"":""bbbbbbbbb"",
""value"":""bbbbbbbbb""
},
{
""label"":""cccccccccccc"",
""value"":""cccccccccccc""
}
]
}
]";
var arrays = new[] { json };
var objectsAsStrings = new List<string>();
foreach (var array in arrays)
{
var tokens = JsonConvert.DeserializeObject<IEnumerable<JToken>>(array);
foreach (var token in tokens)
{
objectsAsStrings.Add(token.ToString());
}
}
var json = "yourjson";
var jsonnew = "[" + json+ "]";
var list = JsonConvert.DeserializeObject<dynamic>(jsonnew);
var result = new List<string>();
foreach (var item in list)
{
var str = JsonConvert.SerializeObject(item);
result.Add(str);
}
You can use this , too. 您也可以使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.