Parsing dynamic JSON string into string in C# using JSON.NET
This is my first little project on C# and JSON. I'm asked to parse a JSON file into the following: I'm trying to create a windows form whose body will contain the content of a JSON string in the following format:
Name of the object
(Label) name of the attribute of the object - (TextBox) editable value
(Label) name of the attribute of the object - (TextBox) editable value
...
I have approx 35 attributes per object in the json file and 8 objects. There are around 50 different attributes in total. I have searched for JSON - C# - JSON.NET and read more than 15 questions & answers. Answers had some valuable information for starters like me, but I could not associate the answers with my situation. These are the reasons:
<Dictionary>
data structure. I'm not fully aware of dictionaries and their properties, but by definition i think, dictionaries map 2 values to each other. In my case, 3-5 different objects may have the same attribute, therefore for one attribute there will be multiple values. Besides that, an example I've seen was:
var dict = new JavaScriptSerializer().Deserialize<Dictionary<string,object>>(json); var postalCode = dict["postalcode"];
Since I have around 50 attributes, using this approach is not an option for me: Using the field names one by one like it was done in this example ("postalcode" was one of the attributes in the example json object in the referenced question).
At the first glance, I thought I might parse it on my own using string manipulation. But I want to use the beautiful JSON.NET library. I'm sort of stuck here, having no idea how to get the name of the attribute of the json object and use it and its value in the windows form. I have some ideas in my mind but have no idea how to implement them.
I might parse the json string that contains 8 objects into an object array, each object containing 2D string arrays for their attribute name and value. Then I will convert string into float when editing the value. I need them as strings because I want to use them in the windows form. This is my solution in mind. How can I proceed?
Since you have a dynamic json string, I'd recommend parsing the string as a JObject
. This gives ultimate flexibility as far as processing the JSON object tree:
var parseTree = JsonConvert.DeserializeObject<JObject>("{ a: 2, b: "a string", c: 1.75 }");
foreach (var prop in parseTree.Properties()) {
Console.WriteLine(prop.Name + ": " + prop.Value.ToObject<object>());
}
Another JObject
example:
var parsed = JsonConvert.DeserializeObject<JObject>("{"name":{"a":2, "b":"a string", "c":3 } }");
foreach (var property in parsed.Properties()) {
Console.WriteLine(property.Name);
foreach (var innerProperty in ((JObject)property.Value).Properties()) {
Console.WriteLine("t{0}: {1}", innerProperty.Name, innerProperty.Value.ToObject<object>());
}
}
If you know that you are dealing with a JSON array, you can instead parse as JArray
and then look at each JObject
in the array:
var properties = JsonConvert.DeserializeObject<JArray>("[{a:1}, {b:2, c:3 }]")
.SelectMany(o => ((JObject)o).Properties())
.ToArray();
foreach (var prop in properties) {
Console.WriteLine(prop.Name + ": " + prop.Value.ToObject<object>());
}
For even more flexibility, you can parse as a JToken
.
For that kind of dynamic JSON, you might want to consider something less cumbersome than JSON.NET, such as SimpleJson (https://github.com/facebook-csharp-sdk/simple-json). It's just a .cs file that you copy/paste in your project or that you install through NuGet.
It supports "dynamic" and is extremely easy to use.
For instance, if you have a JSON such as:
{
"inputs" : ["a", "b", "c"],
"outputs" : ["c", "d", "e"]
}
You can parse it:
dynamic sys = SimpleJson.DeserializeObject(jsonString);
And then access everything straightaway:
foreach(var attr in sys) {
foreach( var name in attr ) {
Console.WriteLine("In {0} with name: {1}", attr, name);
}
}
You could of course also have accessed by name or index:
Console.WriteLine(sys["inputs"][0]);
which would print 'a'.
链接地址: http://www.djcxy.com/p/20082.html