favoritefastjson, because it only needs to rely on the JDK, all other environments do not need to rely on can be used. And it is very fast. Here I also use it to write out some insights and summarize for your correction.
Speaking of fastjson, we need to understand the concept of serialization: the object information is converted into information that can be transmitted and stored [Baidu Encyclopedia].
When we don't need a field we can use @JSONFiled(serialize=false) to unserialize it, by default both serialize and deserialize are true in the JSONFiled class.
Inside @JSONFiled() there are a lot of parameters that can be set, such as the type of the time @JSONFiled(format="yyyyMMdd HHmmss"), and you can also specify the name to be used when serializing.
-
@Id
-
@Column(name = "id")
-
@JSONField(name = "ID")
-
@GeneratedValue(strategy= )
-
public int getId() {
-
return id;
-
}
For example, we specify the serialized name as "ID" above.
Another advantage of fastjson is that it integrates well with entity classes in java. We define the entity class. Then we return to the front-end use, there are some fields we do not need or front-end does not require the provision of . So we can use the filter to filter it out.
SimplePropertyPreFilter filter = new SimplePropertyPreFilter(,"ID","contractContent","fixtureTime","contractContent","typeName","dealMark","transactionAmount");
return (new BaseModel(Constants.SUCCESS_CODE,fundLists2),filter);
The fields returned in this way are the prescribed fields defined inside our Filter.
When we output the relevant columns in order, we can add the annotation @JSONField(ordinal=0) to the output model class . as follows:
-
@JSONField(ordinal = 0)
-
private String code;
-
@JSONField(ordinal = 1)
-
private Map<String,String> myGroup;
-
@JSONField(ordinal = 2)
-
private Map<String,String> addGroup;
In addition we need to return relevant information when holding an object, it will be parsed incorrectly, for example, parsed into an object type such as $ref, we can do some processing in the parsing (added the red marked part), as follows:
return (new BaseModel(Constants.SUCCESS_CODE,resultList),filter,<strong><span style="color:#ff0000;"></span></strong>);
There are also some less frequently used libraries that are actually quite powerful.JSONPath:
-
package ;
-
-
import ;
-
import ;
-
-
import ;
-
import ;
-
-
public class JSONPathTest {
-
public static void main(String[] args) {
-
List<MyEntity> entities = new ArrayList<MyEntity>();
-
(new MyEntity(1001, "ljw1001"));
-
(new MyEntity(1002, "yan1002"));
-
(new MyEntity(1003, "asw1003"));
-
(new MyEntity(1004, null));
-
-
/**
-
* :: Use fastjson as a query, not sure how efficient this is compared to ID queries
-
* [id in (1001,1003)] Filter elements in a collection by a specific ID
-
* [0,2] Filter the elements in the set by subscript, return the elements with 0 and 2 in the subscript.
-
* [0:2] Filter the elements in the set by subscripts, return the elements 0-2 in the subscripts.
-
* $.name Returns all the names in MyEntity.
-
*
-
*/
-
List<MyEntity> result = (List<MyEntity>)(entities, "[id in (1001,1003)]");
-
List<MyEntity> result1 = (List<MyEntity>)(entities, "[0,2]");
-
List<MyEntity> result2 = (List<MyEntity>)(entities, "[0:2]");
-
List<MyEntity> result3 = (List<MyEntity>)(entities, "$.name");
-
MyEntity myEntity = (0);
-
//(result3);
-
-
-
MyEntity myEntity2 = new MyEntity(1005,"asd1005","String Object");
-
String value = (String)(myEntity2, "$.value");
-
(value);
-
-
//whether there is a value element
-
boolean isContain = (myEntity2, "$.value", ());
-
(isContain);
-
-
int size = (myEntity2, "$");
-
(size);
-
-
}
-
-
}
-
-
class MyEntity{
-
private int id;
-
private String name;
-
private Object value;
-
public MyEntity(){
-
}
-
public MyEntity(int id,String name){
-
this.id = id;
-
this.name = name;
-
}
-
public MyEntity(int id,String name,Object value){
-
this(id,name);
-
this.value = value;
-
}
-
@Override
-
public String toString() {
-
return "MyEntity [hljs-string">", name=" + name + "]";
-
}
-
-
//getter and setter
-
}
Usage of ParseProcess:
-
package ;
-
-
import ;
-
import ;
-
import ;
-
-
import ;
-
import ;
-
import ;
-
-
public class VO {
-
private int id;
-
private Map<String, Object> attributes = new HashMap<String, Object>();
-
public int getId() { return id; }
-
public void setId(int id) { this.id = id;}
-
public Map<String, Object> getAttributes() { return attributes;}
-
-
public static void main(String[] args) {
-
ExtraProcessor processor = new MyExtraProcessor();
-
-
VO vo = ("{\"id\":123,\"value\":\"123456\"}", , processor);
-
(());
-
//value has been forced into an Integer type.
-
(().get("value"));
-
}
-
}
-
-
-
class MyExtraProcessor implements ExtraProcessor, ExtraTypeProvider {
-
public void processExtra(Object object, String key, Object value) {
-
VO vo = (VO) object;
-
().put(key, value);
-
}
-
-
public Type getExtraType(Object object, String key) {
-
if ("value".equals(key)) {
-
return int.class;
-
}
-
return null;
-
}
-
};
Extract collection objects inside JSON strings
Of course, we can also use fastJson to parse JSON strings, for example, we have the following JSON string.
-
{
-
"code": "00",
-
"t": [
-
{
-
"chargeFree": 0,
-
"date": "12-03 19:12",
-
"showProdKeyName": "EUR/USD",
-
"userImg": "http://116.31.94.164:8090/market-master-gateway/resources/images/upload",
-
"verifyFlg": 0,
-
"praiseCount": 0,
-
"readCount": 0,
-
"ifForward": 0,
-
"points": 50,
-
"ifSuccess": 0,
-
"targetPrice": "1.0624"}
-
]
-
}
We want to parse parsing t into an object, using the following:
-
JSONObject jsonObject = (str);
-
-
String data = ("t").toString();
-
List<ViewPointEntity> lists = (data,);
Of course it's okay to omit the intermediate steps, which can be done in a better way below:
List<ViewPointEntity> lists = (List<ViewPointEntity>)((str).get(t));
Extract json and encapsulate it directly into an object
For example, a json object is in the following format:
-
{
-
"ID": 515697,
-
"chargeFree": 0,
-
"commentCount": 0,
-
"confidence": "30"
-
}
You can perform string to object operation with the following code:
ViewPointEntity viewpoint = (msg,);
You can convert the above t into a List, and then you can manipulate it.
Additionally we can make theChromeBrowser to load a JSON recognition plug-in project address: /gildas-lormeau/JSONView-for-Chrome, download and unzip, load the unzipped program to locate the WebContent on it.