Wednesday, February 5, 2014

XML and json must be an analytic field a field you


            
Hello everyone, we usually define the protocol when, like the data flow is defined as XML or json format, parsing, it must be a field in an analytical field it?
There is no better analytical tools, direct serialization or deserialization?
Reply:
dom4j, sax, jdom, can parse XML, do not have to write a text parser.
json with jackson, or google open source json parser is OK to.
Reply:
Very simple data read directly into the data stream is generally a relatively formatted xml file with Dom resolve it on a very simple
json not how it used to see someone with a few lines did not seem to feel
Reply:
Dom efficiency is still quite high
Reply:
Under Android should Pull, I just wrote a blog "under Android with Pull parsing and generating XML"
Landlord can see: http://blog.csdn.net/joneeky/article/details/ 17080127
Reply:
references, 4th Floor joneeky reply: under
Android should Pull, I just wrote a blog "under Android with Pull parsing and generating XML"
Landlord can see: http://blog.csdn.net/joneeky/article/details/ 17080127

I've been using this approach to resolve, but because the XML structure, we need to write often, the definition of a structure, it is necessary to write one annoying.
Reply:
json words jackson gson, xml, then jaxb
Reply:
reference to the 6th floor b87936260 reply:
json words jackson gson, xml, then jaxb

jaxb and gson support directly deserialize it? And support list yet?
Reply:
According to the radiation mechanism, write your own point code like ah
 / ** 
* The json converted into a collection
* @ Param jsonArr can be JSONObject object or json string
* @ Param clazz
* @ Return
* @ Throws Exception
* /
public List jsonToList (Object jsonArr, Class clazz) throws Exception {
List list = new ArrayList ();
JSONArray json = (jsonArr instanceof JSONArray (JSONArray) jsonArr:? New JSONArray (jsonArr.toString ()));
for (int i = 0; i list.add (jsonToBean (json.get (i), clazz));
}
return list;
}


 / ** 
* Convert the json object
* @ Param jsonObj can be JSONObject object or json string
* @ Param clazz
* @ Return
* @ Throws Exception
* /
public T jsonToBean (Object jsonObj, Class clazz) throws Exception {
JSONObject json = (jsonObj instanceof JSONObject (JSONObject) jsonObj:? New JSONObject (jsonObj.toString ()));
Object object = clazz.newInstance (); Method [] methods = clazz.getMethods ();
for (int i = 0; i Object value = null; try {value = json.get (getProp (methods [i]));} catch (Exception e) {}
setProp (methods [i], object, value);
}
return (T) object;
}

 / ** 
* To the properties of the object assignment
* @ Param method
* @ Param object
* @ Param value
* @ Return
* /
public Object setProp (Method method, Object object, Object value) {
if (null == method | | null == object | | null == value | | isPropMethod (method, SET_PROP)!) {return object;}
String pn = method.getParameterTypes () [0] getName (), val = value.toString ();.
if (pn.equals ("short") | | pn.equals (Short.class.getName ())) {
try {method.invoke (object, Short.parseShort (val));} catch (Exception e) {}
} Else if (pn.equals ("int") | | pn.equals (Integer.class.getName ())) {
try {method.invoke (object, Integer.parseInt (val));} catch (Exception e) {}
} Else if (pn.equals ("long") | | pn.equals (Long.class.getName ())) {
try {method.invoke (object, Long.parseLong (val));} catch (Exception e) {}
} Else if (pn.equals (String.class.getName ())) {
try {method.invoke (object, val);} catch (Exception e) {}
} Else if (pn.equals ("float") | | pn.equals (Float.class.getName ())) {
try {method.invoke (object, Float.parseFloat (val));} catch (Exception e) {}
} Else if (pn.equals ("double") | | pn.equals (Double.class.getName ())) {
try {method.invoke (object, Double.parseDouble (val));} catch (Exception e) {}
} Else if (pn.equals (BigDecimal.class.getName ())) {
try {method.invoke (object, new BigDecimal (val));} catch (Exception e) {}
} Else if (pn.equals ("boolean") | | pn.equals (Boolean.class.getName ())) {
try {method.invoke (object, "1" equals (val) true: ("0" equals (val) false:.?.? Boolean.parseBoolean (val)));} catch (Exception e) {}
} Else if (pn.equals (Date.class.getName ())) {
try {
if (value.getClass (). getName (). equals (Date.class.getName ())) {
method.invoke (object, value);
} Else {
method.invoke (object, val.length () <= 10 DateUtil.get () formatDate () parse (val):?.... DateUtil.get () formatDateTime () parse (val));
}
} Catch (Exception e) {}
} Else if (pn.equals (Map.class.getName ())) {
try {method.invoke (object, jsonToMap (value));} catch (Exception e) {}
}
return object;
}

 / ** 
* Access to the property name
according to set or get methods* @ Param method
* @ Return
* /
public String getProp (Method method) {
if (isPropMethod (method, SET_PROP) | | isPropMethod (method, GET_PROP)) {
String mn = method.getName ();
return mn.substring (3, 4) toLowerCase () + mn.substring (4, mn.length ());.
}
return "";
}

No comments:

Post a Comment