Data storage (3) -- JSON Data Processing and -- json Data Processing

Source: Internet
Author: User

Data storage (3) -- JSON Data Processing and -- json Data Processing
JSON is a lightweight data exchange format with good readability and ease of writing. It can exchange data between different platforms. JSON adopts a highly compatible text format and has behaviors similar to the C language system. JSON can convert a Java object to a json string and convert a json string to Java. It is more lightweight than XML, and Json is relatively lightweight and simple to use. JSON data format is widely used in Android for communication between clients and servers. It is very convenient for network data transmission and resolution.

JSONObject-this is the basic unit in the system for JSON definition. It contains a pair of Key/Value values.
JSONArray -- it represents a group of ordered values. The format of toString output is enclosed in square brackets. Values are separated by commas (,) ([value1, value2, value3]).
JSONStringer -- this class can help you quickly and conveniently create JSONtext. Its biggest advantage is that it can reduce program exceptions caused by format errors. referencing this class can automatically create JSON text in strict accordance with the JSON syntax rules (syntaxrules. Each JSONStringer object can only create one JSON text.
JSONTokener -- json parsing class

JSONException -- exceptions in json


The following describes how to convert the Book object in data storage (2) to a String object in Json format.

public static String ObjectToJsonString(List<Book> books) throws JSONException{JSONStringer stringer = new JSONStringer();stringer.object();stringer.key("books");stringer.array();for(int i=0;i<books.size();i++){stringer.object();stringer.key("book:name").value(books.get(i).getName());stringer.key("book:id").value(books.get(i).getId());stringer.key("book:price").value(books.get(i).getPrice());stringer.key("book:publisher").value(books.get(i).getPublisher());stringer.key("book:count").value(books.get(i).getCount());stringer.endObject();}stringer.endArray();stringer.endObject();return stringer.toString();}

Convert String to Book object

public static List<Book> jsonStringToObject(String str) throws JSONException{List<Book> books = new ArrayList<Book>();JSONTokener jsonTokener = new JSONTokener(str);JSONObject jsonObject = (JSONObject) jsonTokener.nextValue();JSONArray array =jsonObject.getJSONArray("books");for(int i =0;i<array.length();i++){Book book = new Book();JSONObject temp = ((JSONObject) array.get(i));book.setName(temp.getString("book:name"));book.setId(temp.getString("book:id"));book.setPrice(temp.getString("book:price"));book.setPublisher(temp.getString("book:publisher"));book.setCount(temp.optInt("book:count"));books.add(book);}return books;}
Note:

GetXXX () can convert the value of the key to be obtained to the specified type. If the key cannot be converted or there is no value, JSONException is thrown.
OptXXX () is also the value of the key to be obtained to the specified type. If the key cannot be converted or there is no value, the value provided by the user or the default value is returned.



How to Write java programs to read and store json data? Is it possible to use the json-simple package? How can this problem be solved?

Json-simple can be used to achieve the desired effect: However, if there are several groups of data, the strings must be concatenated, or only one group of data can be processed.
Example:
String str = "[,]";
JSONArray arr = (JSONArray) JSONValue. parse (str );
For (int I = 0; I <arr. size (); I ++ ){
JSONObject o = (JSONObject) arr. get (I );
System. out. println (o. get ("name "));
}


Mysql Stored Procedure processing in json format

This tb1.. Are these tables database tables or html table tables?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.