如何在 C# 中反序列化多个 json? [英] How to deserialize multiple json in C#?

查看:39
本文介绍了如何在 C# 中反序列化多个 json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过多个 json 字符串将一些信息加载到我的 Xamarin 应用程序中.当我运行应用程序时,它给了我这个错误:

<块引用>

Newtonsoft.Json.JsonSerializationException: '无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List`1[APIPost]' 因为该类型需要 JSON数组(例如 [1,2,3])正确反序列化.要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或更改反序列化类型,使其成为普通的 .NET 类型(例如,不是像整数这样的原始类型,而不是像这样的集合类型)数组或列表可以从 JSON 对象反序列化.JsonObjectAttribute 也可以添加到类型中以强制它从 JSON 对象反序列化.路径 'postid',第 1 行,位置 9.'

获取json的代码:

HttpClient client = new HttpClient();var result = await client.GetStringAsync($"{APIConfig.Uri}/post/getPost/{APIConfig.Token}/{User.ID}/{User.Token}");列表response = JsonConvert.DeserializeObject>(result);foreach (APIPost post 响应){ //代码  }

APIPost 类:

类 APIPost{公共字符串状态 { 获取;放;}公共字符串 postid { 获取;放;}公共字符串用户 ID { 获取;放;}公共字符串图像 { 获取;放;}公共字符串标题 { 获取;放;}公共字符串位置 { 获取;放;}公共字符串日期 { 获取;放;}公共字符串喜欢 { get;放;}}

这是我得到的json:

<代码>{"postid": "2",用户名":2","image": "asdasdasd","caption": "asdasd",位置":空,"日期": "2019-07-29 20:24:28",喜欢":4"}{"postid": "1",用户名":2","image": "susfdfjsadv","captation": "这只是一个测试.",位置":空,"日期": "2019-07-29 19:58:04",喜欢":2"}

解决方案

问题不在于 C# 或您正在使用的 JSON 序列化库.您从服务器接收的 JSON 无效.因此,没有标准的 JSON 解析库可以成功解析它.您要么需要自己编写,要么更正 JSON 以使其有效.

一个有效的对象看起来像:

<代码>{"postid": "2",用户名":2","image": "asdasdasd","caption": "asdasd",位置":空,"日期": "2019-07-29 20:24:28",喜欢":4"}

一个有效的对象数组如下所示:

<预><代码>[{"postid": "2",用户名":2","image": "asdasdasd","caption": "asdasd",位置":空,"日期": "2019-07-29 20:24:28",喜欢":4"},{"postid": "1",用户名":2","image": "susfdfjsadv","captation": "这只是一个测试.",位置":空,"日期": "2019-07-29 19:58:04",喜欢":2"}]

在同一结构中没有多个 JSON"这样的东西.你要么有一个有效的结构,要么没有.你当然可以有多个结构,但你不能像这样将它们全部混合成一个.

简而言之...修复服务器端代码以发送有效响应.

I'm loading some information to my Xamarin application through multiple json strings. When I run the application, it gives me this error:

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[APIPost]' 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 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 'postid', line 1, position 9.'

Code to get the json:

HttpClient client = new HttpClient();
var result = await client.GetStringAsync($"{APIConfig.Uri}/post/getPost/{APIConfig.Token}/{User.ID}/{User.Token}");
List<APIPost> response = JsonConvert.DeserializeObject<List<APIPost>>(result);

foreach (APIPost post in response)
{  //Code  }

Class APIPost:

class APIPost
{
    public string state { get; set; }
    public string postid { get; set; }
    public string userid { get; set; }
    public string image { get; set; }
    public string captation { get; set; }
    public string location { get; set; }
    public string date { get; set; }
    public string likes { get; set; }
}

This is the json I get:

{
    "postid": "2",
    "userid": "2",
    "image": "asdasdasd",
    "captation": "asdasd",
    "location": null,
    "date": "2019-07-29 20:24:28",
    "likes": "4"
}{
    "postid": "1",
    "userid": "2",
    "image": "susfdfjsadv",
    "captation": "This is just a test.",
    "location": null,
    "date": "2019-07-29 19:58:04",
    "likes": "2"
}

解决方案

The problem isn't with C# or with the JSON serialization library you're using. The JSON you're receiving from the server is invalid. As such, no standard JSON parsing library can be expected to successfully parse it. You'll either need to write your own, or correct the JSON to be valid.

A valid object would look like:

{
  "postid": "2",
  "userid": "2",
  "image": "asdasdasd",
  "captation": "asdasd",
  "location": null,
  "date": "2019-07-29 20:24:28",
  "likes": "4"
}

And a valid array of objects would look like:

[
  {
    "postid": "2",
    "userid": "2",
    "image": "asdasdasd",
    "captation": "asdasd",
    "location": null,
    "date": "2019-07-29 20:24:28",
    "likes": "4"
  },
  {
    "postid": "1",
    "userid": "2",
    "image": "susfdfjsadv",
    "captation": "This is just a test.",
    "location": null,
    "date": "2019-07-29 19:58:04",
    "likes": "2"
  }
]

There's no such thing as "multiple JSON" within the same structure. You either have a valid structure or you don't. You can certainly have multiple structures, but you can't have them all mashed together into one like that.

In short... fix the server-side code to send a valid response.

这篇关于如何在 C# 中反序列化多个 json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆