1

I have a Json like this :

[{"id":"54718","title":"Khaleda to visit China","corres":"Special Correspondent","details":"DHAKA: On a 7-day visit, opposition BNP Chairperson Khaleda Zia will leave Dhaka for China on October 14.","photo":"2012October\/SM\/Khaleda-new-sm20121003132805.jpg"}] 

To parse this Json , so far I have done :

public class Attributes
{
    [JsonProperty("id")]
        public string ID{ get; set; }
        [JsonProperty("title")]
        public string TITLE { get; set; }
        [JsonProperty("corres")]
        public string CORRES { get; set; }
        [JsonProperty("details")]
        public string  DETAIL { get; set; }
        [JsonProperty("photo")]
        public string LINK { get; set; }
}

public class DataJsonAttributeContainer
{
    public List<Attributes> NewsList{ get; set; }
    //public Attributes attributes { get; set; }
}

public static T DeserializeFromJson<T>(string json)
{   //I'm getting the error here     
    T deserializedProduct = JsonConvert.DeserializeObject<T>(json);
    return deserializedProduct;
}

& In my code :

 void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        //parse data
            var container = DeserializeFromJson<DataJsonAttributeContainer>(e.Result);

            //load into list
            for (i = 0; i < container.NewsList.Count ; i++)
            {
                newData[i] = new data();
                newData[i].id = container.NewsList[i].ID;
                newData[i].title = container.NewsList[i].TITLE;
                newData[i].price = container.NewsList[i].CORRES;
                newData[i].image = container.NewsList[i].DETAIL;
                newData[i].link = container.NewsList[i].LINK;
            }

The Problem is : container is getting the json from web server which I can see at debugger , but it's shoeing an exception while deserializing . Can anybody help please ? The Exception I'm getting :

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'BanglaNewsPivot.MainPage+DataJsonAttributeContainer' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.Path '', line 1, position 1.

1 Answer 1

5

Your json is an array (not a single object containing array). Calling your DeserializeFromJson as below

var attrs = DeserializeFromJson<List<Attributes>>(e.Result);

is enough.

--EDIT--

foreach (var attr in attrs)
{
     Console.WriteLine("{0} {1}", attr.ID, attr.TITLE);
}
Sign up to request clarification or add additional context in comments.

6 Comments

Can you please elaborate ? Where to put that code exactly ? I hope you won't mind since I am very new to c#.
@FahimAhmed just replace var container = DeserializeFromJson<DataJsonAttributeContainer>(e.Result); with the code above. Result(attrs) will be a list of Attributes. (No container object)
replaced & now getting the error at loading the values into a list . i.e newData[i].corres = container.NewsList[i].CORRES; at NewsList
Basically my question is how to retieve data from it now ?
@FahimAhmed See the edit. OK you are new to c# but you don't show any effort to learn. Just use VS's debugger and see the content of attrs
|

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.