0

I try to convert from json to c# class object, cause my final step will be put this all data into local db. I have no problem with taking this data like one big string, so the Url is good typed, but it's not my goal.

this is the json data, here we have two objects

[{"id":"1","category":"1","name":"good 1","prize":"12.3","prize2":"13.4","elements":"row,column,paper","secid":"2131","description":"nice","quality":"best","dateofcoming":"2013-12-20 18:08:50","date":"2013-12-20 00:00:00"},
{"id":"2","category":"2","name":"good","prize":"14.3","prize2":"15.4","elements":"up,down,left","secid":"2132","description":"nc","quality":"best","dateofcoming":"2013-12-20 18:10:55","date":"2013-12-20 00:00:00"}]

I try by this way which I found in the book:

  private void getBookData()
    {
        // we creating the request details to the site which give us 
        // back the json data 
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("here is my adress");
       request.Accept = "and header";
        // starting the request
        request.BeginGetResponse(callback_with_food_info, request);
    }

    private void callback_with_food_info(IAsyncResult ar)
    {

        HttpWebRequest request = (HttpWebRequest)ar.AsyncState;
        // we get the response
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(ar);

        DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(ListOfObjects));

        Stream responseStream = response.GetResponseStream();

        ListOfObjects listOfobj = (ListOfObjects)deserializer.ReadObject(responseStream);
        String fsdfsdf = "how how how";



    }

and in the row when we ReadObject

 ListOfObjects listOfobj = (ListOfObjects)deserializer.ReadObject(responseStream);

I get exception "System.InvalidCastException", my List class look like this and it was created by http://json2csharp.com/

  public class DetailsOfObject
   {  
    public string id { get; set; }
    public string category { get; set; }
    public string name { get; set; }
    public string prize { get; set; }
    public string prize2 { get; set; }
    public string elements { get; set; }
    public string secid { get; set; }
    public string description { get; set; }
    public string quality { get; set; }
    public string dateofcoming { get; set; }
    public string date { get; set; }
}

public class ListOfObjects 
{
    public DetailsOfObject list {get; set;}
}

Any advice how to finally convert this json data

3
  • 3
    I'm pretty sure if you use the wcf data contract serializer your target serialized object needs to have data contract attributes for it to work. Commented Dec 31, 2013 at 14:53
  • 1
    Just use this type List<DetailsOfObject> to deserialize not ListOfObjects Commented Dec 31, 2013 at 14:55
  • works, thx a lot pliz create a answer from this comment and i will accept Commented Dec 31, 2013 at 15:15

1 Answer 1

1

You need to add Data attributes to model

 [DataContract]
 public class DetailsOfObject
 {
    [DataMember] 
    public string id { get; set; }

and change deserialize line

List<DetailsOfObject> listOfobj = (List<DetailsOfObject>)deserializer.ReadObject(responseStream);
Sign up to request clarification or add additional context in comments.

2 Comments

chenging deserialize line was enough to make this app works properly. L.B was faster with solution if he doesn't publish the answer I accept yours ;)
@MyWay dellywheel needs some reps. I won't post an answer.

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.