简体   繁体   English

Xamarin Newtonsoft.Json.JsonSerializationException:

[英]Xamarin Newtonsoft.Json.JsonSerializationException:

I have a problem whit deserialize Json Object: 我在反序列化Json Object时遇到问题:

Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (eg {"name":"value"}) into type 'System.Collections.Generic.List`1[xxx.Models.FollowerResponseModel]' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly. Newtonsoft.Json.JsonSerializationException:无法将当前JSON对象(例如{“ name”:“ value”})反序列化为类型'System.Collections.Generic.List`1 [xxx.Models.FollowerResponseModel]',因为该类型需要JSON数组(例如[1,2,3])正确反序列化。 To fix this error either change the JSON to a JSON array (eg [1,2,3]) or change the deserialized type so that it is a normal .NET type (eg not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. 要解决此错误,可以将JSON更改为JSON数组(例如[1,2,3]),也可以更改反序列化类型,使其成为普通的.NET类型(例如,不像整数这样的原始类型,也不像这样的集合类型)数组或列表),可以从JSON对象反序列化。 JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. 还可以将JsonObjectAttribute添加到类型中,以强制其从JSON对象反序列化。 Path 'users', line 2, position 10. 路径“用户”,第2行,位置10。

{"result":{"status":200,"response":{"data":{"users":[
{"uin":223,"login":"tttttt","uin_follows":true,"follow_of_uin":false,"blocked":false},
{"uin":225,"login":"hggjhjj","uin_follows":false,"follow_of_uin":true,"blocked":true},
{"uin":226,"login":"testestefy","uin_follows":false,"follow_of_uin":false,"blocked":true}
],"version":"1"}}}}

My FollowersResponModel 我的关注者响应模型

public class FollowerResponseModel
{
    [JsonProperty("users")]
    public List<UserFollowersModel> Users { get; set; }

    [JsonProperty("version")]
    public int Version { get; set; }
}

public class UserFollowersModel
{
    [JsonProperty("uin")]
    public int Uin { get; set; }

    [JsonProperty("login")]
    public string Login { get; set; }

    [JsonProperty("uin_follows")]
    public bool UinFollows { get; set; }

    [JsonProperty("follow_of_uin")]
    public bool FollowOfUin { get; set; }

    [JsonProperty("blocked")]
    public bool Blocked { get; set; }
}

How should my FollowersModel class look like? 我的FollowersModel类应如何显示?

My method GetFollowers... 我的方法GetFollowers ...

public async Task<List<FollowerModel>> GetFollowersBlockedList(int version)
{
    var request = CreateHttpRequest(string.Format(FOLLOWERS_BLOCKED_URL), HttpMethod.Get, true);

    var response = await CallRequestAsync(request, HttpContentType.ApplicationJson);

    if (response == null) return null;

    var stream = await response.Content.ReadAsStreamAsync();
    var content = Tools.ConvertStreamToString(stream);

    if (string.IsNullOrEmpty(content)) return null;

    var responseModel = JsonConvert.DeserializeObject<ResponseModel>(content);

    if (response.IsSuccessStatusCode)
    {
        _log.MessageInDebug("OK");

        var data = responseModel.Result.Response.Data.ToString();

        var list = JsonConvert.DeserializeObject<List<FollowerResponseModel>>(data);

        if (list == null || list.Count() == 0) return null;

        return list.Select(x => new FollowerModel(x)).ToList();
    }

    return null;
}

FollowerResponseModel already contains a List<User> , so you do not need to deserialize a List<FollowerResponseModel> FollowerResponseModel已经包含List<User> ,因此您不需要反序列化List<FollowerResponseModel>

var follow = JsonConvert.DeserializeObject<FollowerResponseModel>(data);

if (follow.Users == null || follow.Users.Count() == 0) return null;

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

相关问题 Newtonsoft.Json.JsonSerializationException - Newtonsoft.Json.JsonSerializationException 错误:Newtonsoft.Json.JsonSerializationException - Error: Newtonsoft.Json.JsonSerializationException 抛出了Newtonsoft.Json.JsonSerializationException - Xamarin - Newtonsoft.Json.JsonSerializationException has been thrown - Xamarin xamarin.forms-Newtonsoft.Json.JsonSerializationException:转换值时出错 - xamarin.forms - Newtonsoft.Json.JsonSerializationException: Error converting value 用户代码未处理Newtonsoft.Json.JsonSerializationException - Newtonsoft.Json.JsonSerializationException was unhandled by user code Twitterizer TwittterTimeline NewtonSoft.JSON.JsonSerializationException问题 - Twitterizer TwittterTimeline NewtonSoft.JSON.JsonSerializationException problems 如何处理Newtonsoft.Json.JsonSerializationException? - How to handle Newtonsoft.Json.JsonSerializationException? Newtonsoft.Json.JsonSerializationException: C# - Newtonsoft.Json.JsonSerializationException: C# Newtonsoft.Json.JsonSerializationException: &#39;无法反序列化当前的 JSON 数组 - Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array Newtonsoft.Json.JsonSerializationException 反序列化 JSON 响应 C# - Newtonsoft.Json.JsonSerializationException in deserializing the JSON response C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM