Parse data in array format into JSON-formatted data

Source: Internet
Author: User

In the process of programming, the data transmission format, will affect the development efficiency and late code maintenance, and now many of the JS in support of JSON format data,

such as angular, nodejs. This article mainly explains the parsing of the array (array) Form data. Data in other formats will be written in subsequent articles.

(1) Defines a class jsonarray that resolves an array.

public class Jsonarray {
@SuppressWarnings ("Unchecked")
public static String tojsonstring (List paramlist) {
If param is null or "", return
if (Null==paramlist | | 0==paramlist.size ()) {
return "NULL";
}
StringBuffer sb = new StringBuffer ();
Boolean Isfirstele = true;//list parameter is not the first parameter
Iterator Iterator = Paramlist.iterator ();
Sb.append ("[");
while (Iterator.hasnext ()) {
Check is first element
if (Isfirstele) {
Isfirstele = false;
}else{
Sb.append (",");
}

Object element = Iterator.next ();
if (null==element) {
Sb.append ("null");
}else{
Sb.append (jsonvalue.tojsonstring (Element));
}
}
Sb.append ("]");
return sb.tostring ();
}

}

(2) Define a class Jsonvalue that parses various types of data.

Package com.ityongman.base;

Import java.util.List;
Import Java.util.Map;


public class Jsonvalue {
public static String tojsonstring (Object paramobject) {
if (null==paramobject) {
return "NULL";
}
if (paramobject instanceof String) {
Return "\" + Escape ((String) paramobject) + "\" ";
}

if (paramobject instanceof Double) {
if ((Double) paramobject). Isinfinite () | | (Double) paramobject). IsNaN ()) {
return "NULL";
}
return paramobject.tostring ();
}
if (paramobject instanceof Float) {
if ((Float) paramobject). Isinfinite () | | ((Float) paramobject). IsNaN ()) {
return "NULL";
}
return paramobject.tostring ();
}
if (paramobject instanceof number) {
return paramobject.tostring ();
}
if (paramobject instanceof Jsonaware) {
Return ((Jsonaware) paramobject). toJSONString ();
}
if (paramobject instanceof Map) {
Return jsonmap.tostring ((MAP) paramobject);
}
if (paramobject instanceof List) {
Return jsonarray.tojsonstring ((List) (Paramobject));
}

return paramobject.tostring ();
}

private static string Escape (String paramstring) {
if (null = = paramstring) {
return null;
}
StringBuffer sb = new StringBuffer ();
Escape (PARAMSTRING,SB);
return sb.tostring ();
}

static void Escape (String paramstring, StringBuffer sb_param) {
TODO auto-generated Method Stub

for (int i=0;i<paramstring.length (); i++) {
int asciindex = Paramstring.charat (i);
Switch (asciindex) {
Case 8://\b
Sb_param.append ("\\b");
Break
Case 9://\t--Horizontal tab
Sb_param.append ("\\t");
Break
Case 12://\f
Sb_param.append ("\\f");
Break
Case 10://"
Sb_param.append ("\\n");
Break
Case 13://"
Sb_param.append ("\\r");
Break
Case 34://"--File delimiter
Sb_param.append ("\\\");
Break
Case 47://'--closed single quote
Sb_param.append ("\\/");
Break
Case 92://\--Backslash
Sb_param.append ("\\\\");
Break
Default
if (((asciindex >= 0) && (asciindex <= 31)) | | ((asciindex >= 127) && (Asciindex <= 159)) | | ((Asciindex >= 8192) && (Asciindex <= 8447))) {
String str = integer.tohexstring (asciindex);
Sb_param.append ("\\u");
for (int k = 0; k < 4-str.length (); k++) {
Sb_param.append (' 0 ');
}
Sb_param.append (Str.touppercase ());
}else{
Sb_param.append (Asciindex);
}
Break
}
}

}

}

(3) Common data types are list, map,string,double, Float, number; The above code does not handle the map, so the following is provided for the processing class jsonmap of the map.

Package com.ityongman.base;

Import Java.util.Iterator;
Import Java.util.Map;

public class Jsonmap {

public static String toString (Map parammap) {
//TODO
if (Null==parammap | | 0==parammap.size ()) {
return ' null ';        
}
StringBuffer sb = new StringBuffer ();
Boolean Isfirstele = true;
Iterator Iterator = Parammap.entryset (). Iterator ();        
Sb.append ("{");
          while (Iterator.hasnext ()) {
if (isfirstele) {
Isfirstele = false;
          }else{
Sb.append (",");
}
Map.entry entity = (map.entry) iterator.next ();
toJSONString (string.valueof (Entity.getkey ()), Entity.getvalue (), SB);        
}

Sb.append ("}");
return sb.tostring ();
}

private static string toJSONString (String paramstring, Object paramvalue,
StringBuffer Sb_param) {
Sb_param.append ("\" ");
if (null==paramstring | | 0==paramstring.length ()) {
Sb_param.append ("null");
}else{
Jsonvalue.escape (Paramstring,sb_param);
}
Sb_param.append ("\" "). Append (": ");
Sb_param.append (jsonvalue.tojsonstring (paramvalue));

return sb_param.tostring ();
}
}

(4) above involves a jsonaware class, it is an interface, many other classes to implement (implements) it, the following is the specific code.

Package com.ityongman.base;

Public abstract Interface Jsonaware {
Public abstract String tojsonstring ();
}

Summary: The conversion of data formats is a common problem, especially in JSON format, which brings us a lot of convenience, so the provider of this kind of helper class.

Parse data in array format into JSON-formatted data

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.