3

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.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<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.

2 Answers 2

2

It looks like you're trying to deserialize the response as an array (response would be [...]) instead of a single object (response, as shown, is {...}). Change your code to:

var model = JsonConvert.DeserializeObject<NewsModel.RootObject>(data);

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

Comments

0

Try this code.

using System.Net;

string requestUrl = String.Format("http://content.guardianapis.com/search?q=putin&api-key=6392a258-3c53-4e76-87ec-e9092356fa74");
string json = new WebClient().DownloadString(requestUrl);

var jss = new JavaScriptSerializer();
var dict = jss.Deserialize<Dictionary<string, dynamic>>(json.ToString());
object result = dict["results"];

foreach (object items in result as System.Collections.ArrayList)
{
    Dictionary<string, object> item = (Dictionary<string, object>)items;
    string type = item["type"];
    string sectionId = item["sectionId"];
    ...
    ...
    ...
    //or
    //foreach(object item in items) { }
} 

Comments

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.