[W3cschool tydef]
What is JSON?
JSON refers to the JavaScript Object Notation (JavaScript Object Notation)
JSON is a lightweight text data exchange format.
JSON is independent of the Language *
JSON is self-descriptive and easy to understand.
* JSON uses JavaScript syntax to describe data objects, but JSON is still independent of the language and platform. The JSON parser and JSON library support many different programming languages.
JSON syntax is a subset of JavaScript syntax.
Why use JSON
When sending and receiving information in an asynchronous application, you can select plain text and XML as the data format. This phase of understanding Ajax discusses another useful data format, JavaScript Object Notation (JSON), and how to use it to move data and objects more easily in applications.
Simply put, JSON can convert a set of data represented in a javascript object to a string, and then it can be easily passed between functions, alternatively, the string is transmitted from the Web Client to the server in an asynchronous application. This string looks a bit odd (several examples will be seen later), but javascript can easily explain it, and JSON can represent a more complex structure than name/value pairs.
Features
Similar to XML
JSON is plain text
JSON is "self-descriptive" (human readable)
JSON has a hierarchical structure (Values exist in values)
JSON can be parsed using JavaScript
JSON data can be transmitted using Ajax
Differences from XML
No end tag
Shorter
Faster read/write speed
Ability to parse using the built-in JavaScript eval_r () method
Use Arrays
Do not use reserved words
Use JSON
Read JSON strings
Use eval_r () to process JSON strings
Json c ++ API is a good blog, complete.
Http://hi.baidu.com/jiangyangw3r/blog/item/bfedc1ce59b23e0c92457e14.html
An example of C ++ playing a JSON package
# Include
Class jsonguard
{
Public:
Jsonguard (json_object * OBJ): m_obj (OBJ ){}
~ Jsonguard (){
Json_object_put (m_obj );
}
PRIVATE:
Json_object * m_obj;
};
Void testjson ()
{
Cout <"test JSON begin! "<Endl;
Json_object * OBJ = json_object_new_object ();
Jsonguard (OBJ );
// Input a student's JSON format information
Int id = 1;
Json_object_object_add (OBJ, "ID", json_object_new_int (ID ));
String name ("huangfengxiao ");
Json_object_object_add (OBJ, "name", json_object_new_string (name. c_str ()));
Json_object * subobj = json_object_new_object (); // subobj
String mathadd ("A-404 ");
Json_object_object_add (subobj, "math", json_object_new_string (mathadd. c_str ()));
String chineseadd ("A-404 ");
Json_object_object_add (subobj, "chineseadd", json_object_new_string (chineseadd. c_str ()));
Json_object_object_add (OBJ, "subjectaddr", subobj );
Const char * jsdata = json_object_to_json_string (OBJ );
Cout <jsdata <Endl;
Cout <"test JSON end! "<Endl;
}