简体   繁体   English

如何解析 json 对象,以便正确获取所有值

[英]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 &quot;admin&quot; 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 &quot;admin&quot; 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 打开它。

  1. Copy ur Json Code.复制你的 Json 代码。
  2. Click on this and it generate your Code automatically for you:) click here点击它,它会自动为你生成你的代码:)点击这里

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.

相关问题 如何确保所有客户端都能正确解析数据库DateTime字符串? - How do I ensure that all clients can correctly parse a database DateTime string? 我如何获取JSON数组的所有值 - How can i get all values of JSON array 如何正确制作对象列表的列表(主要),以便将项目添加到列表时,不会将其添加到列表(主要)中的所有列表? - How can I make a List(main) of Lists of Objects correctly so that when an item is added to a list, it is not added to all the lists in the List(main)? C#中的JSON:如何在某个类下面的json字符串中获取所有对象? - JSON in C#: How do I get all objects in a json string below a certain class? 如何在 scope 中保留 C# 对象,以便我可以在整个程序中访问和更改值? - How do I keep C# objects in scope so I can access and change values throughout my program? 我如何从json文件中获取json对象的列表 - How do i get a list of json objects from json file 我如何使用Newtonsoft.json将JSON字符串正确解析为C#中的对象? - How do I correctly parse a JSON string using Newtonsoft.json to an object in C#? 如何获取特定链接文本的href值到列表中,以便可以将其更改为完整的URL? - How do I get href values of a specific Link Text into a list so I can change them into a full URL? 如何仅从RestSharp获取JSON对象列表? - How do I just get a list of JSON objects from RestSharp? 如何获取两个 Point 对象之间的所有点? - How do I get all the points between two Point objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM