I am using the sample code provided by Brian Rogers on How do I use JSON.NET to deserialize into nested/recursive Dictionary and List? to convert a downloaded JSON file into a list of dictionaries (because I know that's what format it is in) and then return the value of the dictionary inside the list as text:
public static class JsonHelper
{
public static object Deserialize(string json)
{
return ToObject(JToken.Parse(json));
}
private static object ToObject(JToken token)
{
switch (token.Type)
{
case JTokenType.Object:
return token.Children<JProperty>()
.ToDictionary(prop => prop.Name,
prop => ToObject(prop.Value));
case JTokenType.Array:
return token.Select(ToObject).ToList();
default:
return ((JValue)token).Value;
}
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btn1_Click(object sender, RoutedEventArgs e)
{
string wordType = "verb";
var url = "http://api.wordnik.com:80/v4/word.json/" + txtBox.Text + "/definitions?limit=5&partOfSpeech=" + wordType + "&api_key=aaaa946871985c2eb2004061aba0695e00190753d6560ebea";
var jsontext = new WebClient().DownloadString(url);
object worddata = JsonHelper.Deserialize(jsontext);
txtBlock.Text = worddata[0];
}
}
The main focus is on a worddata
variable. It is some sort of object but not a list of dictionaries. If it is any help I tried printing worddata
to the console and it says System.Collections.Generic.List'1[System.Object]
.
Essentially I am trying to do what the json.load
function does in python.
Example unparsed JSON:
[
{
"textProns": [],
"sourceDictionary": "wiktionary",
"exampleUses": [],
"relatedWords": [],
"labels": [
{
"type": "usage",
"text": "nautical"
}
],
"citations": [],
"word": "cat",
"attributionUrl": "http://creativecommons.org/licenses/by-sa/3.0/",
"attributionText": "from Wiktionary, Creative Commons Attribution/Share-Alike License",
"partOfSpeech": "verb",
"text": "To hoist (the anchor) by its ring so that it hangs at the cathead.",
"score": 0.0
},
{
"textProns": [],
"sourceDictionary": "wiktionary",
"exampleUses": [],
"relatedWords": [],
"labels": [
{
"type": "usage",
"text": "nautical"
}
],
"citations": [],
"word": "cat",
"attributionUrl": "http://creativecommons.org/licenses/by-sa/3.0/",
"attributionText": "from Wiktionary, Creative Commons Attribution/Share-Alike License",
"partOfSpeech": "verb",
"text": "To flog with a cat-o'-nine-tails.",
"score": 0.0
},
{
"textProns": [],
"sourceDictionary": "wiktionary",
"exampleUses": [],
"relatedWords": [],
"labels": [
{
"type": "register",
"text": "slang"
}
],
"citations": [],
"word": "cat",
"attributionUrl": "http://creativecommons.org/licenses/by-sa/3.0/",
"attributionText": "from Wiktionary, Creative Commons Attribution/Share-Alike License",
"partOfSpeech": "verb",
"text": "To vomit something.",
"score": 0.0
},
{
"textProns": [],
"sourceDictionary": "wiktionary",
"exampleUses": [],
"relatedWords": [],
"labels": [
{
"type": "grammar",
"text": "transitive"
},
{
"type": "field",
"text": "computing"
}
],
"citations": [],
"word": "cat",
"attributionUrl": "http://creativecommons.org/licenses/by-sa/3.0/",
"attributionText": "from Wiktionary, Creative Commons Attribution/Share-Alike License",
"partOfSpeech": "verb",
"text": "To apply the cat command to (one or more files).",
"score": 0.0
},
{
"textProns": [],
"sourceDictionary": "wiktionary",
"exampleUses": [],
"relatedWords": [],
"labels": [],
"citations": [],
"word": "cat",
"attributionUrl": "http://creativecommons.org/licenses/by-sa/3.0/",
"attributionText": "from Wiktionary, Creative Commons Attribution/Share-Alike License",
"partOfSpeech": "verb",
"text": "To dump large amounts of data on (an unprepared target) usually with no intention of browsing it carefully.",
"score": 0.0
}
]
Desired result:
"To hoist (the anchor) by its ring so that it hangs at the cathead."