Create a projectCreate a MAVEN project to introduce the jar package required to parse JSON. The Pom.xml configuration is as follows:
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactid>json-lib</ artifactid>
<version>2.4</version>
<!--specify JDK version-->
<classifier>jdk15< /classifier>
</dependency>
parsing Java Objects
public static void Main (string[] args)
{
int age = n;
Name name = new name ();
Name.setfirstname ("Zhang");
Name.setlastname ("San");
list<string> Aihao = new arraylist<string> ();
Aihao.add ("Pashan");
Aihao.add ("movies");
person who = new person ();
Person.setname (name);
Person.setage (age);
Person.setaihao (Aihao);
Jsonobject Jo = jsonobject.fromobject (person);
System.out.println (Jo.tostring ());
}
Parsing the above object, the console prints the JSON string as follows:
{"Age": "Aihao": ["Pashan", "movies"], "name": {"FirstName": "Zhang", "LastName": "San"}
format a JSON object string
algorithm:The traversal of the input string, chasing characters
1, get the current character.
2, if the current character is the front bracket, the front curly braces do the following processing:
(1) If there is a previous character, and the character is ":", Print: NewLine and indent character strings.
(2) Print: current character.
(3) The front bracket, the front curly brace, must be wrapped in the following line. Printing: Wrapping.
(4) Each occurrence of a front bracket, a front curly brace, and an increase in the number of indents once. Print: New line indent.
(5) the next cycle.
3. If the current character is a rear bracket, the following curly braces are treated as follows:
(1) The rear bracket, the back curly brace, must be wrapped in front of the line. Printing: Wrapping.
(2) Each occurrence of a rear bracket, a curly brace, and a reduction in the number of indents once. Print: Indent.
(3) Print: current character.
(4) If there are characters behind the current character, and the character is not ",", Print: line break.
(5) Continue the next cycle.
4, if the current character is a comma. Wrap the comma, and indent, without changing the indent count.
5, printing: the current character.
Code:
Package com.qidou.dmp.tools;
/** * This class provides a way to format a JSON string.
* Methods of this class Formatjson JSON strings to facilitate viewing of JSON data. * <p> For example: * <p>json string: ["Yht", "Xzj", "Zwy"] * <p> formatted as: * <p>[* <p>   ; "Yht", * <p> "XZJ", * <p> "Zwy" * <p
] * * <p> using the algorithm is as follows: * <p> to the input string, chasing the character of the traversal * <p>1, get the current character.
* <p>2, if the current character is the front bracket, the front curly braces are treated as follows: * <p> (1) If there is a previous character, and the character is ":", Print: NewLine and indent character strings.
* <p> (2) Print: current character. * <p> (3) front brackets, curly braces, must be wrapped after line.
Printing: Wrapping. * <p> (4) Each occurrence of a front bracket, a curly brace, and an increase in indent times.
Print: New line indent.
* <p> (5) for the next cycle. * <p>3, if the current character is a rear bracket, the following curly braces are handled as follows: * <p> (1) rear brackets, curly braces, must be wrapped in front of the line.
Printing: Wrapping. * <p> (2) each occurrence of a rear bracket, after the curly braces, reduction of the number of times.
Print: Indent.
* <p> (3) Print: current character.
* <p> (4) If there is a character after the current character, and the character is not ",", Print: Line wrap.
* <p> (5) continue the next cycle. * <p>4, if the current character is a comma.
Wrap the comma, and indent, without changing the indent count.
* <p>5, printing: current character. * * @author Yanghaitao * @version [version number, September 29, 2014] */public class JsOnformattool {/** * unit indent string.
* * private static String space = "";
/** * Returns the formatted JSON string.
* * @param JSON unformatted JSON string.
* @return formatted JSON string.
*/Public String Formatjson (string json) {stringbuffer result = new StringBuffer ();
int length = Json.length ();
int number = 0;
Char key = 0;
traverses the input string.
for (int i = 0; i < length; i++) {//1, get the current character.
Key = Json.charat (i); 2, if the current character is the front bracket, the front curly braces do the following processing: if (key = = ' [] |
(Key = = ' {')]
{//(1) If there is a previous character, and the character is ":", Print: NewLine and indent character strings.
if ((i-1 > 0) && (json.charat (i-1) = = ': ')) {result.append (' \ n ');
Result.append (indent (number));
//(2) Print: current character.
Result.append (key); (3) The front bracket, the front curly brace, must be wrapped in the following line.
Printing: Wrapping.
Result.append (' \ n '); (4) Each occurrence of a front bracket, a front curly brace, and an increase in the number of indents once.
Print: New line indent.
number++;
Result.append (indent (number));
(5) the next cycle.
Continue //3 If the current character is a rear bracket, the following curly braces are handled as follows: if ((key = = ']) | | (key = = =)) {//(1) rear bracket, back curly brace, must be wrapped before line.
Printing: Wrapping.
Result.append (' \ n '); (2) Each occurrence of a rear bracket, a curly brace, and a reduction in the number of indents once.
Print: Indent.
number--;
Result.append (indent (number));
(3) Print: current character.
Result.append (key);
(4) If there are characters behind the current character, and the character is not ",", Print: line break. if (((i + 1) < length) && (Json.charat (i + 1)!= ', ')) {result.append (' \ n ')
; }//(5) Continue to followRing.
Continue }//4, if the current character is a comma.
Wrap the comma, and indent, without changing the indent count.
if ((key = = ', ')) {result.append (key);
Result.append (' \ n ');
Result.append (indent (number));
Continue
}//5, Printing: current character.
Result.append (key);
return result.tostring (); /** * Returns a specified number of indentation strings.
Each time indent three spaces, that is, space.
* * @param number indent times.
* @return A String that specifies the number of indents.
* * Private String indent (int number) {StringBuffer result = new StringBuffer ();
for (int i = 0; i < number; i++) {result.append (spaces);
return result.tostring (); }
}
Test Cases:
public static void Main (string[] args)
{
Jsonformattool tool = new Jsonformattool ();
int age = n;
Name name = new name ();
Name.setfirstname ("Zhang");
Name.setlastname ("San");
list<string> Aihao = new arraylist<string> ();
Aihao.add ("Pashan");
Aihao.add ("movies");
person who = new person ();
Person.setname (name);
Person.setage (age);
Person.setaihao (Aihao);
Jsonobject Jo = jsonobject.fromobject (person);
System.out.println (Jo.tostring ());
System.out.println (Tool.formatjson (jo.tostring ()));
}
Output:
{"Age": "Aihao": ["Pashan", "movies"], "name": {"FirstName": "Zhang", "LastName": "San"}
{
"age": "
Aihao":
[
"Pashan",
"movies"
],
"name":
{
"firstName": "Zhang" ,
"LastName": "San"
}
}