I'm trying to read a Json string in C#, but I'm having trouble figuring out just how to parse the string into C#. Say I have the following Json string
[
{
"AppName": {
"Description": "Lorem ipsum dolor sit amet",
"Value": "1"
},
"AnotherAppName": {
"Description": "consectetur adipisicing elit",
"Value": "String"
},
"ThirdAppName": {
"Description": "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua",
"Value": "Text"
},
"Application": {
"Description": "Ut enim ad minim veniam",
"Value": "100"
},
"LastAppName": {
"Description": "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat",
"Value": "ZZZ"
}
}
]
I want to parse that into an arraylist or dictionary, using a format like
descriptionList["AppName"] = "Lorem ipsum dolor sit amet";
valueList["AppName"] = "1";
I've been toying around with Json.Net but the examples I've seen don't give me a clear idea of how I should do this. What's the best way to achieve this? Cant this be done like in jQuery, using a foreach statement?
What you are trying to deserialize to a Dictionary is actually a Javascript object serialized to JSON. In Javascript, you can use this object as an associative array, but really it's an object, as far as the JSON standard is concerned.
So you would have no problem deserializing what you have with a standard JSON serializer (like the .net ones, DataContractJsonSerializer and JavascriptSerializer) to an object (with members called AppName, AnotherAppName, etc), but to actually interpret this as a dictionary you'll need a serializer that goes further than the Json spec, which doesn't have anything about Dictionaries as far as I know.
One such example is the one everybody uses: JSON .net
There is an other solution if you don't want to use an external lib, which is to convert your Javascript object to a list before serializing it to JSON.
var myList = [];
$.each(myObj, function(key, value) { myList.push({Key:key, Value:value}) });
now if you serialize myList to a JSON object, you should be capable of deserializing to a List<KeyValuePair<string, ValueDescription>>
with any of the aforementioned serializers. That list would then be quite obvious to convert to a dictionary.
Note: ValueDescription being this class:
public class ValueDescription
{
public string Description { get; set; }
public string Value { get; set; }
}
json:
[{"ew":"vehicles","hws":["car","van","bike","plane","bus"]},{"ew":"countries","hws":["America","India","France","Japan","South Africa"]}]
c# code: to take only a single value, for example the word "bike".
//res=[{"ew":"vehicles","hws":["car","van","bike","plane","bus"]},{"ew":"countries","hws":["America","India","France","Japan","South Africa"]}]
dynamic stuff1 = Newtonsoft.Json.JsonConvert.DeserializeObject(res);
string Text = stuff1[0].hws[2];
Console.WriteLine(Text);
output:
bike
you can try with System.Web.Script.Serialization.JavaScriptSerializer
:
var json = new JavaScriptSerializer();
var data = json.Deserialize<Dictionary<string, Dictionary<string, string>>[]>(jsonStr);
Instead of an arraylist or dictionary you can also use a dynamic. Most of the time I use EasyHttp for this, but sure there will by other projects that do the same. An example below:
var http = new HttpClient();
http.Request.Accept = HttpContentTypes.ApplicationJson;
var response = http.Get("url");
var body = response.DynamicBody;
Console.WriteLine("Name {0}", body.AppName.Description);
Console.WriteLine("Name {0}", body.AppName.Value);
On NuGet: EasyHttp
Source: Stackoverflow.com