The processing of JSON in JAVA, Python, and C + + can be done through a third-party library, which is not supported by the language itself. Here's a comparison of the advantages and disadvantages of these three languages in processing JSON in a common scenario. The following is a representation of the Java Jackson Library, the Python Simplejson Library, and the C + + Jsoncpp library as their respective languages
1. Convert the object to a JSON string
S1 class instances that are well defined for their respective languages
Java:
Objectmapper objmapper = new Objectmapper ();
Objmapper.writevalueasstring (S1)
Python:
If you want to convert a custom object to a JSON string, you also need to define a function in Python that converts the object to the Dict type:
def convert_student (obj):
Dict = {}
Dict.update (obj.__dict__)
Return Dict
Json.dump (obj,default=convert_student)
C++:
Jsoncpp does not support functions that serialize objects directly into JSON strings, it can only implement JSON serialization of objects by manually building JSON nodes
Json::value Root;
root["name"]= "XXX"
root["Age"]=12
Root.tostyledstring ()
From the above three different languages can be seen, because Java itself has the concept of Java beans, that is, there is a common way to get object properties, so in Java can easily convert objects into JSON strings, and C + + obviously cannot provide this generic concept, So the developer can only convert the class to a defined Json::value, and then Jsoncpp from Jsonvalue. Because of the built-in Dict data type, this data type is naturally suitable for processing JSON transformations, so as long as the class-to-dict data type conversion function is implemented, the conversion to the JSON string can be accomplished.
2. Convert the JSON string to a class
Java:
Student RT = Objmapper.reader (Student.class). ReadValue (JSONSTR);
Python:
def rebuild_student (Pairs):
Stu = Student ()
stu.__dict__ = Pairs
Return Stu
Json.loads (Jsonstr, object_hook=rebuild_student)
C++:
Json::reader Reader;
Json::value Value;
Reader.parser (Str,value)
From the class to the JSON string analysis, we are not difficult to derive from the JSON string to the class conversion, the same Java can be converted directly, Python needs to rely on the dict type, and C + +, only honest themselves from the json::value to convert.