简体   繁体   English

C# 中 JSON 的嵌套反序列化

[英]Nested deserialization of JSON in C#

I am trying to retrieve the contents of "data" from my JSON response.我正在尝试从我的 JSON 响应中检索“数据”的内容。 I believe I've created the classes correctly but I'm messing up the deserialization for the TechProfile class.我相信我已经正确地创建了这些类,但是我搞砸了 TechProfile class 的反序列化。 I get an Exception when trying to set the 'techProfile' variable.尝试设置“techProfile”变量时出现异常。

Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: P. Path '', line 0, position 0.' Newtonsoft.Json.JsonReaderException:'解析值时遇到意外字符:P.路径'',第 0 行,position 0。

I have reluctantly added a ToString() at the end of the JSONMainResponse.Data parameter, as it doesn't compile without it.我不情愿地在 JSONMainResponse.Data 参数的末尾添加了一个 ToString(),因为没有它就无法编译。

My JSON:我的 JSON:

{
"success":true,
"msg":"",
"data":[
    {"categoryName":"Target platform","option_category_id":"1","skill_optionID":"1","option_type":"Scale","name":"Desktop apps","value":"2"},
    {"categoryName":"Target platform","option_category_id":"1","skill_optionID":"2","option_type":"Scale","name":"Mobile apps","value":"2"},
    {"categoryName":"Target platform","option_category_id":"1","skill_optionID":"3","option_type":"Scale","name":"Server apps","value":"5"},
    {"categoryName":"Target platform","option_category_id":"1","skill_optionID":"4","option_type":"Scale","name":"Hardware dev","value":"2"},
    {"categoryName":"Target platform","option_category_id":"1","skill_optionID":"5","option_type":"Scale","name":"Web dev","value":"0"}
    ]
}

Main code:主要代码:

    string result = postData("http://test.com/test.php");

            JSONMain JSONMainResponse = JsonConvert.DeserializeObject<JSONMain>(result);

            string success = JSONMainResponse.Success;
            string msg = JSONMainResponse.Msg;
            TechProfile techProfile = JsonConvert.DeserializeObject<TechProfile>(JSONMainResponse.Data.ToString());

The classes:课程:

    public partial class JSONMain
        {
            [JsonProperty("success")]
            public string Success { get; set; }

            [JsonProperty("msg")]
            public string Msg { get; set; }

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


    public partial class TechProfile
        {
            [JsonProperty("categoryName")]
            public string CategoryName { get; set; }

            [JsonProperty("option_category_id")]           
            public string OptionCategoryId { get; set; }

            [JsonProperty("skill_optionID")]
            public string SkillOptionId { get; set; }

            [JsonProperty("option_type")]
            public string OptionType { get; set; }

            [JsonProperty("name")]
            public string Name { get; set; }

            [JsonProperty("value")]
            public string Value { get; set; }
        }

I was over-egging it a bit.我有点过分了。

First change made to the JSONMain class:对 JSONMain class 进行的第一次更改:

public List<TechProfile> Data { get; set; }

instead of...代替...

public TechProfile[] Data { get; set; }

Then for the main code, instead of trying to deserialize again, I simply declared a new List of TechProfiles based on JSONMainResponse.Data.然后对于主要代码,我没有尝试再次反序列化,而是简单地声明了一个基于 JSONMainResponse.Data 的新 TechProfile 列表。 See below见下文

string success = JSONMainResponse.Success;
string msg = JSONMainResponse.Msg;
List<TechProfile> data = JSONMainResponse.Data;

foreach(TechProfile tp in data)
{
    string categoryName = tp.CategoryName;
}

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

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