简体   繁体   中英

Converting JSON to C# class

I have this JSON string:

[{"fkp_keyword":"CLI_RID"},
 {"fkp_keyword":"DOC_NAME"},
 {"fkp_keyword":"FILENAME"},
 {"fkp_keyword":"PRINT_DATE"},
 {"fkp_keyword":"EVENT_CODE"},
 {"fkp_keyword":"CONFL_RID"},
 {"fkp_keyword":"PROGRAM_CODE"},
 {"fkp_keyword":"CES"},
 {"fkp_keyword":"DISTR"},
 {"fkp_keyword":"REC_DATE"},
 {"fkp_keyword":"REC_RID"},
 {"fkp_keyword":"PFL_RID"},
 {"fkp_keyword":"DES"},
 {"fkp_keyword":"CER_RID"}
]

I need to convert it into a List of the class kw below.

Definitions:

public class kw
{
    public string fkp_keyword { get; set; }
}

But this code:

List<kw> header = new List<kw>();
using (WebClient client = new WebClient())
{
    client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
    string result = client.DownloadString(parms);
    header = JsonConvert.DeserializeObject<List<kw>>(result);
}

The call returns the JSON string above but when trying to convert it, the code above returns this exception: Error converting value to type 'System.Collections.Generic.List[LA.Models.kw]

Update

I changed the definitions to this:

public class kwList
{
    public kw[] Property1 { get; set; }
}
public class kw
{
    public string fkp_keyword { get; set; }
}

and the code to this:

kwList header = new kwList();
using (WebClient client = new WebClient())
{
    client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
    string result = client.DownloadString(parms);
    header = JsonConvert.DeserializeObject<kwList>(result);
}

But now I'm getting this Exception:
Could not cast or convert from System.String to LicenseeArchive.Models.kwList.

What am I doing wrong?

For whatever reason, it appears that the JSON string returned by that URL is double-serialized. That is, it contains extra backslashes to escape all the quotes, which then prevents it from being deserialized properly to an array of objects. That is why you are getting an error.

To work around the problem, you can deserialize it twice: first to unescape the JSON, the second to do the "real" deserialization to your classes. Longer term, you may also wish to contact the provider of the API to see if they will fix their JSON.

List<kw> header = new List<kw>();
using (WebClient client = new WebClient())
{
    client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
    string result = client.DownloadString(parms);
    string unescapedJson = JsonConvert.DeserializeObject<string>(result);
    header = JsonConvert.DeserializeObject<List<kw>>(unescapedJson);
}

Fiddle: https://dotnetfiddle.net/XEULdy

JSON string you provided can be loaded with your first class definition:

public class kw
{
  public string fkp_keyword { get; set; }
}

Example:

string example = "[{\"fkp_keyword\":\"CLI_RID\"}, {\"fkp_keyword\":\"DOC_NAME\"}, {\"fkp_keyword\":\"FILENAME\"}]";
List<kw> kws =  JsonConvert.DeserializeObject<List<kw>>(example);

Maybe you are not providing all details. Or your json string looks different.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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