2

So I am trying to parse some JSON that is returned to me by a third party api that looks something like:

{
    "status":"ok",
    "links":
    [
        {
            "link":
            {
                "link_name":"Sample",
                "link_id":"9999"
            }
        },

    ],//and so on with other nested properties

I have created classes to map the JSON to

    [DataContract]
    public class JsonTestResults
    {
        [DataMember]
        public string status { get; set; }
        [DataMember]
        public IEnumerable<Link> links { get; set; }
    }
    [DataContract]
    public class Link
    {
        [DataMember]
        public string link_name { get; set; }
        [DataMember]
        public string link_id { get; set; }
    }

And I'm pushing the response through this deserializer (taken from this post

public  T Deserialise<T>( string json )
        {
            T obj = Activator.CreateInstance<T>( );
            using (MemoryStream ms = new MemoryStream( Encoding.Unicode.GetBytes( json ) ))
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer( obj.GetType( ) );
                obj = (T)serializer.ReadObject( ms ); 
                return obj;
            }
        }

However, my deserialized results are showing the contents of Link[] as null. (there is a Link object for each one returned, but the link_name and link_id are null.)

I've checked out this, this, this, this and this, but haven't been able to solve this issue. I am looking for a solution that doesn't require a third party library. (per my lead dev).

I don't believe it's a problem with the classes matching the JSON, but I can post the full code if anyone would like to review it.

Community
  • 1
  • 1
Forty-Two
  • 7,535
  • 2
  • 37
  • 54
  • Did you tried to change the Enumerable to a generic list? – lolol Sep 06 '12 at 16:28
  • I'm pretty sure that the bug is where I'm looking. Look: http://stackoverflow.com/questions/12271356/how-do-i-serialize-this-twitter-entity-response-from-the-stream-api – lolol Sep 06 '12 at 16:44

2 Answers2

3

You need one more class to deserialize it correctly

public class JsonTestResults
{
    public string status { get; set; }
    public IEnumerable<TempLink> links { get; set; }
}

public class TempLink
{
    public Link link;
}

public class Link
{
    public string link_name { get; set; }
    public string link_id { get; set; }
}

I tested it with Json.Net and worked.

var obj = JsonConvert.DeserializeObject <JsonTestResults>(json);

JavaScriptSerializer also works

var obj2 = new JavaScriptSerializer().Deserialize<JsonTestResults>(json);
L.B
  • 114,136
  • 19
  • 178
  • 224
  • lolol offered this solution earlier. Using this approach, the contents of links is still null, only now it is link = null rather than link_name and link_id = null. – Forty-Two Sep 06 '12 at 16:48
  • @Forty-Two As I said I tested it before posting. – L.B Sep 06 '12 at 16:50
  • @Forty-Two I'm prety sure it's right too. Maybe there's something wrong with the DataContractJsonSerializer. – lolol Sep 06 '12 at 16:50
  • You're right, (both of you). Apparently it was some problem with my Deserialize method that adding the additional class didn't change anything. However, as you said, it does indeed work with JavaScriptSerializer() – Forty-Two Sep 06 '12 at 16:56
  • Nice @Forty-Two. Fell free to give it for L.B because he sustained his position with more tests than I did. Thank you and have a nice day. – lolol Sep 06 '12 at 17:00
  • For VS2017 and beyond, it is possible to generate the classes from the JSON directly although interestingly, the generated code doesn't compile. If I hand code the TempLink structure, it all works. – Robbie Dee Jan 11 '21 at 13:52
-1

Its a syntax error in the JSON,

In a JSON Array the last element does not have a ',' after it.

 {
"status":"ok",
"links":
[
    {
        "link":
        {
            "link_name":"Sample",
            "link_id":"9999"
        }
    }
],

This will work !!

EJGZ
  • 1
  • 1
    nah, you are wrong. he leaved the comma to show that there are more elements in the json. – lolol Sep 06 '12 at 16:38