1

I am writing a program to access Mediafire's web API and it's all going well, the only issue remaining is the response text in JSON format that I have difficulty parsing.

With API calls like creating a folder, I get a simple response which can be deserialized into a Dictionary<string,Dictionary<string,string>> and searched for values:

{"response":
    {
    "action":"folder\/create.php",
    "name":"blargh",
    "folder_key":"mmttuu769djo0",
    "result":"Success",
    "current_api_version":"2.14"
    }
}

I would use it like this:

Dictionary<string,string> json = DeserializeJSON(text)["response"];
//DeserializeJSON is a method to shorten:
//JsonConvert.DeserializeObject<Dictionary<string,Dictionary<string,string>>(text)

I can then query for json["result"] and whatnot. With other API calls I get complex structures that I'm not sure how to handle. It's basically a bunch of key:value pairs, but some of the values are key:value pairs as well, which can't be put into a dictionary like I'm currently doing. I'm fairly new to C# so I'm not sure what to do here, is there some other data type like a Dictionary which doesn't have static types?

Here's the response:

{"response":
    {
    "action":"upload\/upload.php",
    "doupload":
        {
        "result":"0",
        "key":"89lh7760x4l"
        },
    "server":"live",
    "result":"Success",
    "current_api_version":"2.14"
    }
}

My question would be: What is a good way to get this kind of data into a list that I can query for values?

2
  • 2
    Write your own answer not in the question so those who read may find the "right answer" Commented Apr 14, 2014 at 10:50
  • 1
    I tried at the time but some warning said I have to wait 8 hours to answer my own question, I'll see if I can do it now though. Yep, but now I have to wait 24 hours to accept it! Commented Apr 14, 2014 at 14:37

2 Answers 2

2

What about creating a new class(s) to deal with the json? You can generate classes by using json2csharp using the example json.

public class Doupload
{
    public string result { get; set; }
    public string key { get; set; }
}

public class Response
{
    public string action { get; set; }
    public Doupload doupload { get; set; }
    public string server { get; set; }
    public string result { get; set; }
    public string current_api_version { get; set; }
}

public class RootObject
{
    public Response response { get; set; }
}

Then you can deserialise the json using:

JavaScriptSerializer serializer = new JavaScriptSerializer(); 
var something = serializer.Deserialize<RootObject>(jsonString);
Sign up to request clarification or add additional context in comments.

3 Comments

Hmm... That seems like a pretty good approach but most API calls have unique keys that it returns so I would have to make a class for every API function.
this sounds like a good idea to me, I'd prefer the strongly typed version. How many classes would you really have to create?
There's around 50-60 different API functions to access, you can see what some of them respond with here: mediafire.com/developers I think I'd need a class for each one but I'm not sure.
1

I ended up finding out about the dynamic type - Deserializing the text into a Dictionary<string,dynamic> allows it to have multiple types where some can be dictionaries as well. I can query it as I would expect but I just need to be sure what values are returned with each API call, and I need to cast it to a string.

string upload_key = (string)json["response"]["doupload"]["key"] //89lh7760x4l

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.