简体   繁体   English

通过c#和xamarin中的JSON解析

[英]Parse through JSON in c# and xamarin

I have a json string that I receive in c# that looks like this 我在c#中收到的json字符串看起来像这样

{
"status": "error",
"data": {
    "first_name": ["The first name field is required."],
    "last_name": ["The last name field is required."],
    "email": ["The email field is required."],
    "password": ["The password field is required."]
    }
}

My code looks like this when calling the JSON string. 调用JSON字符串时,我的代码看起来像这样。 I am trying to parse the data to pull the values out of the Status and the Data. 我试图解析数据以从状态和数据中提取值。 However I am receiving errors whenever I get beyond making the request and to the actual parsing of the object. 但是,每当我超出发出请求和实际解析对象时,我都会收到错误。

var client = new HttpClient();
var response = await client.SendAsync(request);

var content = await response.Content.ReadAsStringAsync();

var resp = JsonConvert.DeserializeObject<Json_StatusResponse>(content);
var test = resp.first_name;

and my model looks like this: 我的模型看起来像这样:

public class Json_StatusResponse
{
    public string message { get; set; }
    public string first_name { get; set; }
    public string last_name { get; set; }
    public string password { get; set; }
    public string email { get; set; }
    public string status { get; set; }
    //public string[][] data { get; set; }
    public List<Dictionary<string, object>> data { get; set; }

    public class Data
    {
        public List<string> first_name { get; set; }
        public List<string> last_name { get; set; }
        public List<string> email { get; set; }
        public List<string> password { get; set; }
    }

    public class RootObject
    {
        public string status { get; set; }
        public Data data { get; set; }
    }

}

For some reason I get 出于某种原因,我得到了

Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.String[][]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'data.first_name', line 1, position 39.

只需使用:

public Dictionary<string, object> data { get; set; }

Your Json_StatusResponse does not match the JSON provided. 您的Json_StatusResponse与提供的JSON不匹配。 you should probably be deserializing to the RootObject instead 你可能应该反序列化到RootObject

I would recommend something like this: 我会推荐这样的东西:

public partial class RootObject
{
    [JsonProperty("status")]
    public string Status { get; set; }

    [JsonProperty("data")]
    public Data Data { get; set; }
}

public partial class Data
{
    [JsonProperty("first_name")]
    public string[] FirstName { get; set; }

    [JsonProperty("last_name")]
    public string[] LastName { get; set; }

    [JsonProperty("email")]
    public string[] Email { get; set; }

    [JsonProperty("password")]
    public string[] Password { get; set; }
}

and moving those nested classes out of Json_StatusResponse The fields that you are using for your Json_StatusResponse are also not required (and from what I can tell, dont do anything) 动人的嵌套类出Json_StatusResponse您正在使用您的字段Json_StatusResponse另外,也不需要(从我可以告诉,不要做任何事情)

you also need to be deserializing the RootObject , not Json_StatusResponse like so: 您还需要反序列化RootObject ,而不是Json_StatusResponse如下所示:

var resp = JsonConvert.DeserializeObject<RootObject>(content);
var test = resp.Data.first_name[0];

Your JSON formatting is wrong "[]" incidate that something is an array. 您的JSON格式错误“[]”表示某些内容是数组。 There your values are just single. 你的价值观只是单一的。

Modify your JSON to: 将您的JSON修改为:

{
"status": "error",
"data": {
    "first_name": "The first name field is required.",
    "last_name": "The last name field is required.",
    "email": "The email field is required.",
    "password": "The password field is required."
    }
}

Also I recommend using JSON formatter website to validate your JSON: 另外,我建议使用JSON格式化程序网站验证您的JSON:

JSON Validator JSON验证器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM