Newtonsoft.Json.JsonReaderException无法识别左方括号"[". [英] Newtonsoft.Json.JsonReaderException does not recognize left square bracket "["

查看:1168
本文介绍了Newtonsoft.Json.JsonReaderException无法识别左方括号"[".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用NewtonSoft库中的DeserializeObject,但是遇到了一个困扰我很多的问题.

I'm trying to use DeserializeObject from the NewtonSoft library but I encoutered a problem that bothers me a lot.

我对该库非常陌生,因此为了更好地理解它,我看了以下

I'm quite new with that Library so to understand it better I took a look at the following example from the official website.

使用该资源,我对其进行了修改以满足自己的需求,这是我的课程:

Using that ressource, I modified it to fit my needs, here is my class :

public class Project
{
    [JsonProperty ("team")]
    public string Team{ get; set; }
    [JsonProperty("details")]
    public string Details { get;  set;}
}

这是代码:

Project project = new Project();
string json_string = File.ReadAllText(@"C:\file.json", Encoding.UTF8);
project = JsonConvert.DeserializeObject<Projets>(json_string);

这是.json文件模板:

And here is the .json file template :

{
   "Team": "nameOfTeam",
   "Details": [
       {
       "detail1": "Unknown",
       "detail2": "Unknown"
       }
   ]
}

问题是,运行时出现以下消息

Problem is, there is an exception during the run-time with the following message

解析值[.时遇到意外字符.路径详细信息",第X行,位置XX.

Unexpected character encountered while parsing value: [. Path 'Details', line X, position XX.

我不明白字符"["在这里可能是个问题,我是否遗漏了一些东西?

I don't understand how the character "[" could be a problem here, am I missing something ?

推荐答案

Details是C#类中的字符串,而它是JSON对象中的数组.要么将其设置为JSON中的字符串,要么将属性设置为IList<string>.

Details is a string in your C# class, while it's an array in the JSON object. Either make it a string in JSON, or make the property an IList<string> instead.

这篇关于Newtonsoft.Json.JsonReaderException无法识别左方括号"[".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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