0

I have a JSON in a web server like this :

{"My Book List": [{"ID":"5","TYPE":"History","TITLE":"Ekannoborti","PRICE":"200","IMAGE":"Ekannoborti.jpg","DOWNLOAD LINK":"http://www.starhostbd.com/"}],"success":3} 

To deserialize it I have done so far :

 public class Attributes
    {
        public string ID{ get; set; }
        public string TYPE { get; set; }
        public string TITLE { get; set; }
        public string  PRICE { get; set; }
        public string IMAGE { get; set; }
        public string DOWNLOADLINK { get; set; }
    }

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

    public static T DeserializeFromJson<T>(string json)
    {
        T deserializedProduct = JsonConvert.DeserializeObject<T>(json);
        return deserializedProduct;
    }

& in my code :

void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        //var deserializedJSON = JsonConvert.DeserializeObject<Attributes>(e.Result);
        var container = DeserializeFromJson<DataJsonAttributeContainer>(e.Result);
        string asd = container.attributes[0].DOWNLOADLINK[0].ToString();
        //string asd = deserializedJSON.DOWNLOADLINK[0].ToString();
    } 

The problem is : From debug wiindow i can see that data is assigned in e.Result but container remains null . How to solve this problem ? Please help !

1
  • With mine and pedro'd modification this code works for me. I can extract the whole link. But you need to use string asd = container.attributes[0].DOWNLOADLINK.ToString(); to obtain the whole link. In your case you obtain only the first character. Commented Oct 1, 2012 at 14:03

3 Answers 3

1

Add a JsonProperty attribute to the attributes property to match the property name in the JSON, like so:

public class DataJsonAttributeContainer
{
    [JsonProperty("My Book List")]
    public List<Attributes> attributes { get; set; }
}

Also, you should add a JsonProperty attribute to the Attributes.DOWNLOADLINK property with "DOWNLOAD LINK" value in order for it to match the JSON property name.

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

3 Comments

Working fine here, but I did notice an error in your sample code above: there is a missing " after the My Book List
sorry ! that was a typo due to copy paste from the actual large json file . Actual file is ok !
You can check here what I did, and it is working fine: gist.github.com/3811718
0

At first glance I think the problem is at the DOWNLOADLINK property. Your server returns "DOWNLOAD LINK" but your property hasn't the space in it's name.

You should define the json representation at your property like this:

[JsonProperty(PropertyName = "DOWNLOAD LINK")]
public string DOWNLOADLINK { get; set; }

Hope this helps.

Comments

0

Actually both the above answers should solve your problem, you have to club them

public class MyBookList
{
    public string ID { get; set; }
    public string TYPE { get; set; }
    public string TITLE { get; set; }
    public string PRICE { get; set; }
    public string IMAGE { get; set; }

    [JsonProperty("DOWNLOAD LINK")]
    public string DOWNLOADLINK { get; set; }
}

public class DataJsonAttributeContainer
{
    [JsonProperty("My Book List")]
    public List<MyBookList> MyBookList { get; set; }

    public int success { get; set; }
}

and also

var container = DeserializeFromJson<DataJsonAttributeContainer>(e.Result);
string asd = container.attributes[0].DOWNLOADLINK.ToString();

Try with these classes. Should work.

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.