尝试解析 JSON 时出现 JsonToken.StartArray 错误



我有一个需要解析的JSON,我正在按如下方式解析它:

JObject json = JObject.Parse(jsonProfile);

JSON 的格式为:

[{ "Id": "mahesh", "GeneralInfo": { "FirstName": "sharma", "LastName": "kanth", "PreferredFirstName": "Akash", "Title": "Designer", "InformalTitle": "Designer", "Gender": "", "Discipline": "", "Department": "Strategy", "BusinessUnit": "", "BrandFunction": "", "ParentAgency": "Public", "Agency": "fish", "AgencyImagePath": "", "Hub": "Public", "SubRegion": "America", "Region": "Americas", "Continent": "North America", "Country": "United States", "State": "Oregon", "Location": "Portland", "Email": "sharma@gmail.com", "SkypeName": "", "Phone": "" }}]

当我运行它时,我收到以下错误:

{"Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1."} 

如何处理"["括号?

如果你知道 json,你可以使用 DeserializeObject 直接解析你的 json:

在线试用!

var root = JsonConvert.DeserializeObject<List<RootObject>>(json);
Console.WriteLine(root[0].GeneralInfo.Email);

输出

sharma@gmail.com

类(用json2csharp生成,但vs也可以做到(

public class GeneralInfo
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string PreferredFirstName { get; set; }
public string Title { get; set; }
public string InformalTitle { get; set; }
public string Gender { get; set; }
public string Discipline { get; set; }
public string Department { get; set; }
public string BusinessUnit { get; set; }
public string BrandFunction { get; set; }
public string ParentAgency { get; set; }
public string Agency { get; set; }
public string AgencyImagePath { get; set; }
public string Hub { get; set; }
public string SubRegion { get; set; }
public string Region { get; set; }
public string Continent { get; set; }
public string Country { get; set; }
public string State { get; set; }
public string Location { get; set; }
public string Email { get; set; }
public string SkypeName { get; set; }
public string Phone { get; set; }
}
public class RootObject
{
public string Id { get; set; }
public GeneralInfo GeneralInfo { get; set; }
}

相关内容

最新更新