7

I'm very new to working with JSON and am dealing wtih a return from an API which I can't change the formatting of. The return example is : (actual urls have been removed)

{
"body":
    {
    "link":
        {"linkurl": ["www.google.com"]}
    },
"error": null,
"message": "Data Retrieved successfully",
"status": true
}

I am using the Newtonsoft.Json library with MVC 3, in VS2010.

My class is:

[JsonObject(MemberSerialization.OptIn)]
    public class LinksJSON
    {
        [JsonProperty]
        public string link{ get; set; }

        [JsonProperty]
        public string message { get; set; }

        [JsonProperty]
        public string error { get; set; }

        [JsonProperty]
        public bool status { get; set; }
    }

I'm deserializing it with :

private static T _download_serialized_json_data<T>(string url) where T : new()
    {
        using (var w = new WebClient())
        {
            var json_data = string.Empty;
            // attempt to download JSON data as a string
            try
            {
                json_data = w.DownloadString(url);
            }
            catch (Exception) { }
            // if string with JSON data is not empty, deserialize it to class and return its instance 
            return !string.IsNullOrEmpty(json_data) ? JsonConvert.DeserializeObject<T>(json_data) : new T();
        }
    }

    public string CheckJSONLink()
    {

        var url = "<api url-removed for security>";

        var outObj = _download_serialized_json_data<LinksJSON>(url);



        return outObj.Link;
    }

However I'm trying to get the value of linkurl, which is an indexed array within Link.

How would I access this information?

1
  • btw, i can access all other fields ( error, message and status with no problem ). Commented Dec 5, 2012 at 18:20

2 Answers 2

2

You did not setup your class to match the JSON, your class says that link is a simple string, you your example shows it as a complex type.

In order to properly deserialize it as you expect, you will have to modify your class to match the JSON. Specifically link should be an instance of a class.

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

1 Comment

Thanks for this answer as well, as it prompted the discussion with their team to get the proper class structure to deserialize.
1

You should use the following classes:

[JsonObject(MemberSerialization.OptIn)]
public class LinksJSON
{
    [JsonProperty]
    public body body { get; set; }

    [JsonProperty]
    public string message { get; set; }

    [JsonProperty]
    public string error { get; set; }

    [JsonProperty]
    public bool status { get; set; }
}

[JsonObject(MemberSerialization.OptIn)]
public class body
{
    [JsonProperty]
    public link link { get; set; }
}

[JsonObject(MemberSerialization.OptIn)]
public class link
{
    [JsonProperty]
    public string[] linkurl { get; set; }
}

1 Comment

This is what I ended up doing: similar to above [JsonObject(MemberSerialization.OptIn)] public class LinksJSON { [JsonProperty] public bodylinkdetails body; [JsonProperty] public bool status; [JsonProperty] public string message; [JsonProperty] public string error; } public class bodylinkdetails { public link link; } public class link { public string[] linkurl; }

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.