0

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?

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

7 Comments

Cannot implicitly convert type 'LicenseeArchive.models.kw' to 'System.Collections.GrnericList<LicenseeArchive.models.kw>'
My bad, I had this wrong: List<kw> header = JsonConvert.DeserializeObject<List<kw>>(result)
Nope, still doesn't work. Still getting first exception shown above.
Does my example code work when you run it somewhere in your project? If yes, then you are not sharing some details. What version of Json.net are you using?
Seems to be extra slashes in the result. I will remove them and try again.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.