I'm trying to parse my json response that look like this one:
{
"response":
{
"status":"ok",
"userTier":"developer",
"total":12075,
"startIndex":1,
"pageSize":10,
"currentPage":1,
"pages":1208,
"orderBy":"relevance",
"results":[
{
"type":"article",
"sectionId":"world",
"webTitle":"Putin-themed cafe opens in Siberia",
"webPublicationDate":"2016-04-13T14:56:42Z",
"id":"world/2016/apr/13/vladimir-putin-themed-cafe-opens-in-siberia",
"webUrl":"http://www.theguardian.com/world/2016/apr/13/vladimir-putin-themed-cafe-opens-in-siberia",
"apiUrl":"http://content.guardianapis.com/world/2016/apr/13/vladimir-putin-themed-cafe-opens-in-siberia",
"sectionName":"World news"
},
{
"type":"article",
"sectionId":"world",
"webTitle":"Spain issues arrest warrants for Russian officials close to Putin",
"webPublicationDate":"2016-05-04T16:59:27Z",
"id":"world/2016/may/04/spain-issues-arrest-warrants-for-russian-officials-close-to-putin",
"webUrl":"http://www.theguardian.com/world/2016/may/04/spain-issues-arrest-warrants-for-russian-officials-close-to-putin",
"apiUrl":"http://content.guardianapis.com/world/2016/may/04/spain-issues-arrest-warrants-for-russian-officials-close-to-putin",
"sectionName":"World news"
}
I created my classes using json2csharp.com (Insert this string: http://content.guardianapis.com/search?q=putin&api-key=6392a258-3c53-4e76-87ec-e9092356fa74) But I had the follow error when I tried to parse it:
var model = JsonConvert.DeserializeObject<List<NewsModel.RootObject>>(data);
Actually I got my data in this way:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://content.guardianapis.com/search?q=putin&api-key=6392a258-3c53-4e76-87ec-e9092356fa74");
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = null;
if (response.CharacterSet == "")
readStream = new StreamReader(receiveStream);
else
readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
string data = readStream.ReadToEnd();
DataSet ds = new DataSet();
StringReader reader = new StringReader(data);
// parse here
}
Something bad with json parsing. Message: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List
1[sample.NewsModel+RootObject]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'response', line 1, position 12.Something bad with json parsing. Message: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List1[sample.NewsModel+RootObject]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'response', line 1, position 12.