Json complex object processing: jsonConfig and jsonjsonconfig

Source: Internet
Author: User

Json complex object processing: jsonConfig and jsonjsonconfig
During conversion between a Json string and a java object, some attribute values are often filtered out. JsonConfig in the json-lib package provides us with this function, the specific implementation methods are as follows. (1) create a JsonConfig instance and configure the attribute exclusion list. (2) use the attribute filter and (3) Write a custom JsonBeanProcessor.

1. Methods for implementing the JSONString Interface

Public class Person implements JSONString {
Private String name;
Private String lastname;
Private Address address;

// Getters & setters

Public String toJSONString (){
Return "{name: '" + name + "', lastname: '" + lastname + "'}";
}
}


2. The second method uses the jsonconfig instance to conveniently add or delete included and excluded attributes.

Public class Person {
Private String name;
Private String lastname;
Private Address address;

// Getters & setters
}

JsonConfig jsonConfig = new JsonConfig ();
JsonConfig. setExclusions (new String [] {"address "});
Person bean = new Person ("jack", "li ");
JSON json = JSONSerializer. toJSON (bean, jsonConfig );

3. Using propertyFilter allows you to control the attributes and classes to be excluded at the same time. This control can also be bidirectional or be applied to json strings to java objects.

Public class Person {
Private String name;
Private String lastname;
Private Address address;

// Getters & setters
}

JsonConfig jsonConfig = new JsonConfig ();
JsonConfig. setJsonPropertyFilter (new PropertyFilter (){

Public boolean apply (Object source/* attribute owner */, String name/* attribute name */, Object value/* attribute value */){
// Return true to skip name
Return source instanceof Person & name. equals ("address ");
}
});
Person bean = new Person ("jack", "li ");
JSON json = JSONSerializer. toJSON (bean, jsonConfig)

4. Let's take a look at JsonBeanProcessor. This method is similar to JsonString implementation. A valid JSONObject representing the original domain class is returned.

Public class Person {
Private String name;
Private String lastname;
Private Address address;

// Getters & setters
}

JsonConfig jsonConfig = new JsonConfig ();
JsonConfig. registerJsonBeanProcessor (Person. class, new JsonBeanProcessor (){

Public JSONObject processBean (Object bean, JsonConfig jsonConfig ){
If (! (Bean instanceof Person )){
Return new JSONObject (true );
}
Person person = (Person) bean;
Return new JSONObject (). element ("name", person. getName (). element ("lastname", person. getLastname ());
}
});

Person bean = new Person ("jack", "li ");
JSON json = JSONSerializer. toJSON (bean, jsonConfig );
Ext parses JSON complex objects

I usually convert the transmitted json into this format {"total": 3, "data": [{"sheet": "Sheet1" },{ "sheet ": "Sheet2" },{ "sheet": "Sheet3"}]}
Then use jsonstore on the js page
Var store = new Ext. data. JsonStore ({
Url: 'Data. jsp ',
Method: 'post ',
Root: 'data ',
TotalProperty: "total ",
Fields: ['sheet '],
Listeners :{
}
});
Store. load ();
If you want to obtain the store sheet, use store. getAt ('0'). get ('sheet ')

How to process json objects

I have previously written that JS object-oriented is essentially a hash table, and a hash table is a specific two-dimensional array form. Therefore, there may be several ways to operate it.

Data operation method, as you have written, most of the arrays use serial numbers for index.
The hash table operation method, which is actually a type of deformation of the array operation method, followed by a name index instead of a forward quotation mark, that is what we call the key-Value Pair-in fact, the key-index method is a type of deformation of the array index method, if data ["id"].

Of course, you can also use the object index method. This test actually creates an object. I also said that the object is essentially a hash, so, the consumption of object creation and hash creation can be considered basically the same. The two are not in many regions!

There is no difference in nature, but it is different for programmers to understand it. This is also a problem of personal programming habits.

Unfortunately, the two examples are not comparable. You create an array, but two objects are created in the array, that is to say, the example is a hybrid method. Because it is anonymous, arrays must also be used.

The example you mentioned later is an object, not an instance. The difference between an object and an instance is somewhat similar to the relationship between classes and objects in the object-oriented language. The subsequent statement is to instantiate the object.

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.