Java self-designed JSON parser

Source: Internet
Author: User
Tags xpath

Of course, there are a lot of good JSON parsing jar packages, such as Jsonobject,gson, and even jsonpath for our testers, but I've implemented it myself (and now it's refactoring).

The idea is this, take this JSON string for example: string j = "{\" a\ ": \" b\ ", \" c\\\ "b\": {\ "a\": 1},\ "d\": [\ "A\", {\ "a\": 3},{\ "a\": 2},\ "\"],\ "e\": \ "b\ "}";

We are able to parse the JSON string successfully with the guarantee that only one string is scanned. So, I first defined a list:private list<object> collections = new Arraylist<object> ();

Collections is used to store all the lists and maps in this JSON string, and when it hits {or [, a new map or list, and then add to the collections:

After storing it, we need a map to record the status of the list or map in the collections, such as whether it is closed, a list or a map, the index:private map<string in collections, map<string, integer>> index = new hashmap<string, map<string, integer>> ();

As you can see, the key of this map is made up of 1 1.1 1.2 1.1.1, so this key can be used to represent the hierarchy of JSON, and of course I use a List to save the order of these keys: private list<string> Level = new arraylist<string> ();

In this way, the data structure is very clear. The next thing to do is to make some judgments in the scan and keep the following points:

1. Encounter [or {a new object and store the object in collections

2. Encounter ' \ \ ' needs to be escaped, you have to jump directly over, and stored in the scanned temporary variables. Like \\{, you don't need a new object.

3. Hit the "symbol, you have to mark, before the next" appears, the scanned out as a string into the temporary variable.

4. Touch: Symbol, start tag is the beginning of a map, and the subsequent occurrences of the string are stored in another temporary variable.

5. Encounter, symbol, will start to deal with the temporary variable, if it is a map of the previous two past variables, a key, a value, all into the corresponding map in collections, if it is a list, then the first temporary variable saved, Put it in the list of the corresponding collections.

6. Encounter] or} symbol, it means that a list or map is resolved completely, it is time to update index of the corresponding list or map of the state.

After parsing, all the data is in the three variables of collections index level, so we just need to set a rule to fetch the data, I use a syntax similar to XPath to take the value, This time only need to parse the XPath path can be derived from this key, and then in the collections to take the value of it!

Here's the code:

Http://files.cnblogs.com/files/zhangfei/zson.rar

Interested can see, if there are any problems, you can contact me!

Java self-designed JSON parser

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.