[英]How do I parse json objects so that I can get all the values correctly
So I got this JSON string which looks like this:所以我得到了这个 JSON 字符串,它看起来像这样:
{"success":false,"errors":[{"name":["Username "admin" has already been taken."],"email":["Email is not a valid email address."]}],"data":[]}
or, more readably:或者,更易读:
{
"success": false,
"errors": [
{
"name": [
"Username "admin" has already been taken."
],
"email": [
"Email is not a valid email address."
]
}
],
"data": []
}
And I want to parse it in a way where I can get all the items in "errors" no matter the name of the error or value.而且我想以一种无论错误或值的名称如何都可以获取“错误”中的所有项目的方式对其进行解析。 Because the objects inside "errors" aren't always going to be "name" and "email" there might be different ones, but they will always be structured like that.
因为“错误”中的对象并不总是“名称”和“电子邮件”,所以可能会有不同的对象,但它们的结构总是如此。
I managed to get this far:我设法做到了这一点:
var theObject = (JObject)JsonConvert.DeserializeObject(theJsonString);
And the goal would be to make it into a dictionary where I could get the name and the value of the error.目标是把它变成一个字典,在那里我可以得到错误的名称和值。 Or just the name and value as one string would also work.
或者只是名称和值作为一个字符串也可以。
Essentially getting a collection of all the errors.基本上得到所有错误的集合。
UPDATE更新
I tried deserializing it into it's own object我尝试将其反序列化为它自己的 object
var theObject = JsonConvert.DeserializeObject<ResponseObject>(responseString);
Where the model looks like this model 看起来像这样
public class ResponseObject
{
[JsonProperty("success")]
public bool Success { get; set; }
//[JsonProperty("errors")]
//public Error[] Errors { get; set; }
[JsonProperty("errors")]
public Dictionary<string, List<string>> errors { get; set; }
[JsonProperty("data")]
public object[] Data { get; set; }
}
Which then throws this error然后引发此错误
{"Cannot deserialize the current JSON array (eg [1,2,3]) into type 'System.Collections.Generic.Dictionary
2[System.String,System.Collections.Generic.List
1[System.String]]' because the type requires a JSON object (eg {"name":"value"}) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON object (eg {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (eg ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.\r\nPath 'errors', line 1, position 27."}{"Cannot deserialize the current JSON array (eg [1,2,3]) into type 'System.Collections.Generic.Dictionary
2[System.String,System.Collections.Generic.List
1[System.String]]' because the type requires a JSON object (eg {"name":"value"}) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON object (eg {"name":"value"}) or将反序列化的类型更改为数组或实现集合接口(例如 ICollection、IList)的类型,例如可以从 JSON 数组反序列化的 List。也可以将 JsonArrayAttribute 添加到该类型以强制其从 Z0ECD11C1D7A287401D148A23BBD7A2F8 数组反序列化。 \r\n路径“错误”,第 1 行,position 27。"}
Errors is a List<Dictionary<string, List<string>>>
错误是一个
List<Dictionary<string, List<string>>>
It looks like this:它看起来像这样:
"errors": [ <--
{
"1": [
"Username "admin" has already been taken."
],
"2": [
"Email is not a valid email address."
]
}
] <--
,
not a Dictionary<string, List<string>>
that will llok like:不是
Dictionary<string, List<string>>
会像:
"errors":
{
"1": [
"Username "admin" has already been taken."
],
"2": [
"Email is not a valid email address."
]
},
A easy solutution for you would be:一个简单的解决方案是:
If u have Visual Studio open it.如果你有 Visual Studio 打开它。
Edit -> Import Json编辑 -> 导入 Json
Good Luck: ,) Sometimes it can be happen that he choose a "false" Datatype.祝你好运:,) 有时他可能会选择“假”数据类型。 but on ur litte json u will be fine.
但是在你的小 json 上你会没事的。
This would be the output:这将是 output:
public class Rootobject
{
public bool success { get; set; }
public Error[] errors { get; set; }
public object[] data { get; set; }
}
public class Error
{
public string[] name { get; set; }
public string[] email { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.