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.

2
  • Did you tried to change the Enumerable to a generic list? Commented Sep 6, 2012 at 16:28
  • I'm pretty sure that the bug is where I'm looking. Look: stackoverflow.com/questions/12271356/… Commented Sep 6, 2012 at 16:44

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

6 Comments

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 As I said I tested it before posting.
@Forty-Two I'm prety sure it's right too. Maybe there's something wrong with the DataContractJsonSerializer.
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()
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.
|
-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 !!

1 Comment

nah, you are wrong. he leaved the comma to show that there are more elements in the json.

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.