json parse parse package --XML

json parsing package (GSON Google, fastJson Ali, jacksonSpring built)

Mobile end (Android, IOS) communication using the http protocol + JSON format go restful style.
Many projects are using Internet protocol Http JSON +
xml heavy WebService service

What is JSON

JSON (JavaScript Object Notation) is a lightweight data interchange format, compared to this data exchange format xml, because xml parsing relatively complicated, but you need to write large chunks of code, so the client and server data interchange format to be exchanged frequently by JSON.

JSON: JavaScript Object Notation (JavaScript Object Notation).

As JSON the braces "{}" surrounded list items, each item between the comma (,) separated, and the project is to use a colon (:) attribute names and values ​​separated. This is a typical representation of the dictionary, also shows that in javascript object dictionary is the structure again. No matter how complex objects can be created and assigned with a JSON code. In the JSON, name / value pairs include field names (in double quotes), write behind a colon, then the value

JSON has two structures

json simply is javascript objects and arrays, it is the object of these two structures and the array of two structures represented by the two structures may complex structures
1, the object: the object is expressed as js "{}" enclosed content, the data structure {key: value, key: value , ...} of the key structure, in object-oriented languages, key attributes for the object, value of the corresponding property value, so it is easily understood the value .key method of getting a property value for the object, the type of the attribute values may be numbers, strings, arrays, several objects.
2, the array: the array is in the js brackets "[]" using an index of the enclosed content, the data structure [ "java", "javascript" , "vb", ...], the values in the same way and all languages, Get the type of field values may be numbers, strings, arrays, several objects.
After the object, an array of two kinds of structures can be combined into a complex data structures.

fastJSON-API


public static final Object parse (String text ); // parse the text as JSON or JSONObject the JSONArray
JSONObject the parseObject (String text) Final public static; // parse the text into JSON JSONObject
public static Final T the parseObject (String text, Class clazz) ; // parse the JSON text for the JavaBean
public static Final JSONArray parseArray (String text); // parse the JSON text into JSONArray
public static Final List parseArray (String text, Class clazz); // parse the JSON text into a JavaBean collection of
public static final String toJSONString (Object object) ; // JSON text into a sequence JavaBean
public static final String toJSONString (Object object , boolean prettyFormat); // a JavaBean sequence as JSON formatted text
public static final Object toJSON (Object javaObject); JavaBean converted to JSONObject or JSONArray.

json parsing, the package

// package json


		User user=new User();
		user.setId(1);
		user.setName("明");
		List<Item> list=new ArrayList<Item>();
		Item item=new Item();
		item.setEmailAddress("[email protected]");
		item.setHomeAddress("成都");
		list.add(item);
		user.setItem(list);
		System.out.println("封裝json"+new JSONObject().toJSONString(user));
		//解析json
		String str="{\"id\":1,\"item\":[{\"emailAddress\":\"[email protected]\",\"homeAddress\":\"成都\"}],\"name\":\"明\"}";
		JSONObject jsonObject = new JSONObject();
		JSONObject parseObject = jsonObject.parseObject(str);
		Integer id = parseObject.getInteger("id");
		String name = parseObject.getString("name");
		JSONArray jsonArray = parseObject.getJSONArray("item");
		System.out.println(id+"===="+name);
		for(Object obj:jsonArray) {
			JSONObject obj2 =(JSONObject) obj;
			String emailAddress = obj2.getString("emailAddress");
			String homeAddress = obj2.getString("homeAddress");
			System.out.println(emailAddress+"----"+homeAddress);
		}

XML parsing

What is XML?
It is an extensible markup language (Extensible Markup Language, abbreviated XML), it is a markup language.
The full name of XML extensible markup language. Data and is mostly used as the configuration file.
XML document consists of about 5 parts logically:
XML declaration: Indicates used XML version, the independence of the information document coding, documentation
document type declaration: pointed DTD XML documents used
elements: the start tag, element content and end tag constituting
Note: in the end, from the content of the document for a description of the role of
processing instructions: to inform other applications to process the instruction data by processing a non-XML format, the format of
  the root element of the XML document is referred to as the document element , and processing instructions which appear on the outside thereof, comments, etc. as a child entity of the document, the root element itself and the sub-elements thereof are inside a tree.


@Test
public void testXML() throws IOException, DocumentException {
	SAXReader saxReader=new SAXReader();
	Document read = saxReader.read(new File("D:\\Eclipse\\WorkSpace\\mavendemo\\src\\main\\resources\\student.xml"));
	Element rootElement = read.getRootElement();
	gitNodes(rootElement);
}
private void gitNodes(Element rootElement) {
	System.out.println("节点名称"+rootElement.getName());
	@SuppressWarnings("unchecked")
	List<Attribute> attributes = rootElement.attributes();
	for(Attribute as:attributes){
		System.out.println("属性"+as.getName()+"---"+"属性值"+as.getText());
	}
	if(!rootElement.equals("")){
		System.out.println("标签属性"+rootElement.getName()+"---"+"标签内容"+rootElement.getText());
	}
	// 使用迭代器遍历
			Iterator<Element> elementIterator = rootElement.elementIterator();
			while (elementIterator.hasNext()) {
				Element next = elementIterator.next();
				gitNodes(next);
			}
}

XML parsing mode?

Dom4j、Sax、Pull

Dom4j and Sax difference

dom4j not suitable for parsing large files, because it is at once the file is loaded into memory, so there may be a memory overflow, sax is based on the event to parse the xml, so he can parse xml large files, it is also because of this, it is possible to xml dom4j flexible CRUD and navigation, but not so strong flexibility sax, sax so often used to parse large xml file, and to some flexible (crud) on the operation of the xml file with dom4j.

Published 26 original articles · won praise 0 · Views 700

Guess you like

Origin blog.csdn.net/YHM_MM/article/details/103965414