Converts JAVA.UTIL.MAP to formatted JSON__JS

Source: Internet
Author: User
<pre name= "code" class= "Java" >import Java.text.SimpleDateFormat;
Import Java.util.Map;

Import Java.util.Set;

	public class Convertmap2json {private static final String QUOTE = "\";
	 /** * Turns the map into a JSON object, something like curly braces * e.g. * {* "eventName": "Name" *} * * @param body * @param tabcount * @param addcomma * @return */public s  Tatic String buildjsonbody (map<string, object> body, int tabcount, Boolean addcomma) {StringBuilder sbjsonbody =
		New StringBuilder ();
		Sbjsonbody.append ("{\ n");
		Set<string> keyset = Body.keyset ();
		int count = 0;
		int size = Keyset.size ();
			for (String key:keyset) {count++;
		Sbjsonbody.append (Buildjsonfield (Key, Body.get (key), Tabcount + 1, Count!= size));
		} sbjsonbody.append (Gettab (Tabcount));
		Sbjsonbody.append ("}");
	return sbjsonbody.tostring ();
	 /** * Generates a JSON field that is like this * e.g.
	* "EventName": "AAA" * @param key * @param value * @param tabcount * @param addcomma * @return * * private static string Buildjsonfield (string key, Object value, int tabcount, Boolean addcomma) {StringBuilder SBJ
		Sonfield = new StringBuilder ();
		Sbjsonfield.append (Gettab (Tabcount));
		Sbjsonfield.append (QUOTE). Append (Key). Append (QUOTE). Append (":");
		Sbjsonfield.append (Buildjsonvalue (value, Tabcount, Addcomma));
	return sbjsonfield.tostring ();
	 /** * Generates a JSON value object * e.g. 
	 * "String" * or * {* "key": "Value" *} * * @param value * @param tabcount * @param addcomma * @return  * * private static String Buildjsonvalue (Object value, int tabcount, Boolean addcomma) {StringBuilder Sbjsonvalue =
		New StringBuilder ();
		if (value instanceof String) {sbjsonvalue.append (QUOTE). Append (Value). Append (QUOTE); else if (value instanceof Integer | | value instanceof Long | | | value instanceof Double) {sbjsonvalue.append (value
		); else if (value instanceof java.util.Date) {sbjsonvalue.append (QUOTE). Append (formatdate) java.util.Datee)). append (QUOTE); else if (Value.getclass (). IsArray () | | | value instanceof java.util.Collection) {sbjsonvalue.append (Buildjsonarray
		(Value, Tabcount, Addcomma)); else if (value instanceof Java.util.Map) {sbjsonvalue.append (Buildjsonbody (JAVA.UTIL.MAP) value, Tabcount, ADDC
		Omma));
		} sbjsonvalue.append (Buildjsontail (Addcomma));
	return sbjsonvalue.tostring (); /** * Generate JSON Array Object * [* "VALUE" *] * or * [* {* "key": "Value" *} *] * * @param VA Lue * @param tabcount * @param addcomma * @return/private static String Buildjsonarray (Object value, int Tabco
		UNT, Boolean Addcomma) {StringBuilder Sbjsonarray = new StringBuilder ();
		Sbjsonarray.append ("[\ n");
		object[] Objarray = null;
		if (Value.getclass (). IsArray ()) {Objarray = (object[]) value; else if (value instanceof java.util.Collection)//The collection class is changed to Object array {Objarray = ((java.util.Collection) value). ToArray ()
		;
		int size = Objarray.length;int count = 0;
			For each object in the loop array {//plus indent, one indent sbjsonarray.append (Gettab (Tabcount + 1)) More than the previous line Obj:objarray;
		Add object values, such as a string "string", or an object Sbjsonarray.append (Buildjsonvalue (obj, Tabcount + 1, ++count!= size));
		} sbjsonarray.append (Gettab (Tabcount));
		Sbjsonarray.append ("]");
	return sbjsonarray.tostring ();
	 /** * Plus indentation, that is, several \ t TODO. * @param count * @return */private static String gettab (int count) {StringBuilder sbtab = new StringBuilder (
		);
		while (count--> 0) {sbtab.append ("T");
	return sbtab.tostring ();
	 }/** * Plus comma * TODO. * * @param addcomma * @return/private static String Buildjsontail (Boolean addcomma) {return addcomma?
	", \ n": "\ n";
	 /** * Format Date * TODO. * * @param date * @return/private static String FormatDate (java.util.Date date) {return new SimpleDateFormat
	("Yyyy-mm-dd HH:mm:ss"). Format (date); }
}


<pre name= "code" class= "Java" >import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;

Import Java.util.Map;

Import Org.junit.Test;
		public class Testconvertmap2json {@Test public void Test () {map<string, object> data = InitData ();
	System.out.println (convertmap2json.buildjsonbody (data, 0, false)); private static map<string, object> InitData () {map<string, object> Map = new hashmap<string, Object
		> ();
		Map.put ("EventName", "Applicationdetailupdateafter");
		Map.put ("source", "11111-22222-33333");
		
		Map.put ("Age", 123.333);
		map<string, string> parammap = new hashmap<string, string> ();
		
		Map.put ("Parameters", Parammap);
		Parammap.put ("RecordID", "rec14-00000-00013");
		
		Parammap.put ("RecordType", "ggg-aaa-cc-dd");
		list<string> countrylist = new arraylist<string> ();
		Map.put ("Country", countrylist);
		Countrylist.add ("the");
		
		Countrylist.add ("USA"); List<map<string, OBJect>> maplist = new arraylist<map<string, object>> ();
		Map.put ("Maplist", maplist);
		map<string, object> tempmap = new hashmap<string, object> ();
		Tempmap.put ("name", "zzz");
		Tempmap.put ("Nick", "TTT");
		Maplist.add (TEMPMAP);
	return map; }
	
}


{"
	maplist": [
		{"
			Nick": "TTT",
			"name": "zzz"
		}
	],
	"source": "11111-22222-33333",
	"Age": 123.333,
	"EventName": "Applicationdetailupdateafter",
	"parameters": {
		"RecordID": " rec14-00000-00013 ",
		" RecordType ":" Ggg-aaa-cc-dd "
	},
	" Country ": [" the ","
		USA "
	]
}


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.