Java Operations JSON data format __json

Source: Internet
Author: User
Tags convert string to json string to json

Packages that need to be introduced:

Jakarta Commons-lang 2.3 
Jakarta commons-beanutils 1.7.0 Jakarta commons-collections 3.2 Jakarta 
Commons-logging 1.1 
ezmorph 1.0.4 
Json-lib-2.4-jdk15.jar


Product.java

public class Product {
	private String product;

	private double price;

	Private Object item;


	Public Object GetItem () {return
		item;
	}

	public void SetItem (Object) {
		This.item = Item;
	}

	Public String getproduct () {return
		product
	}

	public void Setproduct (String product) {
		this.product = product;
	}

	Public double GetPrice () {return price
		;
	}

	public void Setprice (double price) {
		This.price = Price;
	}

}


Jsonutil.java

public class Jsonutil {//{"Product": "Pencil", "Price": "Item": [{"id": 1, "name": "Chen"}]} private static Final String J

	Sostr = "{\ product\": \ "pencil\", \ "price\": 12,\ "item\": [{\ "id\": 1,\ "name\": \ "Chen\"}]} ";

		public static void Main (string[] args) throws IOException {//String System.out.println (String4json ("Key", "20"));
		obj Product p = (product) getobject4jsonstring (JSOSTR, Product.class);

		System.out.println (p.getproduct () + "," + P.getprice ());
		Map map map = Getmap4json (JSOSTR); for (Iterator iterator = Map.entryset (). iterator (); Iterator.hasnext ();)
			{Map.entry Entry = (map.entry) iterator.next ();
		System.out.println (Entry.getkey () + "," + Entry.getvalue ());
		}//Array object[] objects = Getstringarray4json (JSOSTR);
		for (int i = 0; i < objects.length i++) {System.out.println (objects[i]); Convert string to JSON public static string String4json (string key, String value) throws Jsonexception {jsonobject O Bject = new Jsonobject ();
		Object.put (key, value);
	return object.tostring ();  ///From a JSON object character format to get a Java object public static object Getobject4jsonstring (String jsonstring, Class pojocalss) {Object
		Pojo
		Jsonobject jsonobject = Jsonobject.fromobject (jsonstring);
		Pojo = Jsonobject.tobean (Jsonobject, POJOCALSS);
	return Pojo; ///Get a map public static map Getmap4json (String jsonstring) {jsonobject jsonobject = Jsonobject.fromobjec from a JSON expression
		T (jsonstring);
		Iterator Keyiter = Jsonobject.keys ();
		String key;
		Object value;
		Map ValueMap = new HashMap ();
			while (Keyiter.hasnext ()) {key = (String) keyiter.next ();
			Value = Jsonobject.get (key);
		Valuemap.put (key, value);
	return VALUEMAP; ///Get a list of Java objects from the JSON object collection expression public static list Getlist4json (String jsonstring, Class pojoclass) {Jsonarray Jsonar
		Ray = Jsonarray.fromobject (jsonstring);
		Jsonobject Jsonobject;
		Object Pojovalue;
		List List = new ArrayList (); for (int i = 0; i < jsonarray.size (); i++) {JsonobjECT = Jsonarray.getjsonobject (i);
			Pojovalue = Jsonobject.tobean (Jsonobject, Pojoclass);

		List.add (Pojovalue);

	} return list; ///Parse out Java string array public static object[] Getstringarray4json (string jsonstring) {Jsonobject jsonobj = Jsonob from JSON array
		Ject.fromobject (jsonstring);
		Jsonarray Jsonarr = Jsonobj.getjsonarray ("item");
	Return (object[]) Jsonarr.toarray (); }
Related Article

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.