Yuki Kutsuya
Yuki Kutsuya

Reputation: 4088

JSON cannot get data

Hi I'm trying to parse a JSON page in C#, but I can't seem to read some strings, like:

"versions": [
    {
        "date": 1340466559, 
        "dl_link": "http://dev.bukkit.org/media/files/599/534/NoSwear.jar", 
        "filename": "NoSwear.jar", 
        "game_builds": [
            "CB 1.2.5-R4.0"
        ], 
        "hard_dependencies": [], 
        "md5": "d0ce03e817ede87a9f76f7dfa67a64cb", 
        "name": "NoSwear v5.1", 
        "soft_dependencies": [], 
        "status": "Semi-normal", 
        "type": "Release"
    }, 

How could I read that? This is the code I have right now and works for everything else except this:

public static void GetMoreInfo(string plugin)
{
    try
    {
        string url = "http://bukget.org/api/plugin/";

        var wc = new WebClient();
        var json = wc.DownloadString(url + plugin);
        var moreInfo = JsonConvert.DeserializeObject<MoreInfo>(json);

        foreach (var category in moreInfo.categories)
        {
            Categories += category + ", ";
        }
        Categories = Categories.Remove(Categories.Length - 2, 2);
    }
    catch (Exception)
    {

    }
    finally
    {
        if (string.IsNullOrEmpty(Categories))
        {
            Categories = "No data found.";
        }
    }
}

public class MoreInfo
{
    public string[] categories;
}

Upvotes: 0

Views: 2788

Answers (2)

L.B
L.B

Reputation: 116098

How about handling your json dynamically, instead of deserializing to a concrete class?

var wc = new WebClient();
var json = wc.DownloadString("http://bukget.org/api/plugin/test");

dynamic moreInfo = JsonConvert.DeserializeObject(json);

Console.WriteLine("{0} {1} {2}", moreInfo.name, moreInfo.desc, moreInfo.status);
string categories = String.Join(",", moreInfo.categories);
Console.WriteLine(categories);

Or would you prefer the classical approach?

var plugin = JsonConvert.DeserializeObject<Plugin>(json);
string categories = String.Join(",",plugin.categories);

public class Plugin
{
    public List<string> authors;
    public string bukkitdev_link;
    public List<string> categories;
    public string desc;
    public string name;
    public string plugin_name;
    public string status;
    public List<Version> versions;
}

public class Version
{
    public string date;
    public string filename;
    public string name;
    //.......
}

Upvotes: 4

sellmeadog
sellmeadog

Reputation: 7517

I could be wrong, but the JSON you're receiving is an array named versions but you're trying to deserialize it into a MoreInfo object that exposes an array named categories (unless it's just a typo in your example). Have you tried renaming MoreInfo.categories to MoreInfo.versions?

UPDATE

I don't think that would make a difference because the JSON converter doesn't know what to do with each object in the array. You need to provide an object to deserialize each element in the JSON array.

For example, try modifying the MoreInfo object to include matching properties to the JSON string (the elements in the array)

[DataContract]
public class MoreInfo
{
  [DataMember]
  public DateTime date { get; set; }

  [DataMember]
  public string dl_link { get; set; }
  ...
}

And then try to deserialize the JSON string to a List<MoreInfo>:

JsonConverter.DeserializeObject<List<MoreInfo>>(json)

You will then have to modify the rest of your code to work with MoreInfo objects and not strings in an array.

REFER TO THIS ANSWER

See this question, the accepted answer should help you solve your problem: Json.NET: Deserialization with list of objects

Upvotes: 0

Related Questions