C parses an JSON instance
- 2020-10-23 21:11:11
- OfStack
This article describes the C# method of parsing JSON as an example. C# encapsulates the class library for parsing XML and JSON, which is quite convenient to use! Specific usage is as follows:
1. Main classes used:
Mainly use the JavaScriptSerializer class, the class in System Web. Script. Serialization namespace (in System. Web. Extensions. dll), need to take. NET version is modified to. NET Framework 4 (default is. NET Framework 4 Client Profile) can in Add Reference of NET in reference to.
2. For example
. Assume that the JSON file format to be parsed is as follows (other formats can be processed accordingly)
{
"key":1,
"value":"hello"
}
. Define the corresponding class
public class JsonObject
{
public string Key { get; set; }
public string Value{ get; set; }
}
. Then define the JavaScriptSerializer object, and call the Deserialize method of the object to resolve JSON to the JsonObject object defined above
var serializer = new JavaScriptSerializer();
var ret = serializer.Deserialize<JsonObject>(json);
string key = ret.Key;
string value = ret.Value;
Hopefully this article has helped you with your C# programming.