Map and Jsonobject the 2 data structures between the transformations

Source: Internet
Author: User
Tags map data structure

The front desk writes JSON directly: var array = [];

Call method: Array[index], if object, then ["Key"]

var obj = {"A": 123, "B": "Hello", "C": Array}

Calling method: Obj.a or obj["a"]

It's just var.

var str = " {" A ": 123," B ":" Hello "," C ": Array}" Str doesn't have that many methods .

**************************************************

1 Importjava.util.LinkedList;2 ImportJava.util.*;3 ImportJava.util.ListIterator;4 5 ImportNet.sf.json.JSONArray;6 ImportNet.sf.json.JSONObject;7 8 9  Public classa11111111 {Ten      Public Static voidMain (string[] args) { One          AJsonobject json=NewJsonobject (); -Json.put ("name", "Xiaoming"); -Json.put ("Age", "10"); the         //The output JSON is: {"name": "Xiaoming", "Age": "Ten"} -         //JSON is a real data structure that can have these methods Json.isempty () -         //if: The string is not so many ways -  +Jsonarray ja=NewJsonarray (); -Ja.add ("1"); +Ja.add ("2"); AJa.add ("3"); atJa.add (3, JSON); -         //["1", "2", "3", {"name": "Xiaoming", "Age": "Ten"}] - System.out.println (ja.tostring ()); -System.out.println (Ja.get (3));  -         //{"name": "Xiaoming", "Age": "10"} points out is a string, not a data structure -         //System.out.println (Ja.get (3). Get ("age")); The Get method is not available, Ja.get (3). This is a little bit of a way out of everything. inString str = (string) ja.get (3);//Str can receive this string -      to  +         /*Ja.get (3) is essentially a string representation.*/ -         //now Ja is a string, parsed into the structure above theJsonobject jsonobject = Jsonobject.fromobject (Ja.get (3)); *System.out.println (Jsonobject.get ("Age"));//get ten $         Panax Notoginsenghashmap<string, object> data =NewHashmap<string, object>();  -Iterator it =Jsonobject.keys ();  the            //Traverse jsonobject Data, add to map object +             while(It.hasnext ()) A            {   theString key =string.valueof (It.next ());  +String value =(String) jsonobject.get (key);  - data.put (key, value);  $            }   $ System.out.println (data); -                //{name= xiaoming, age=10} -     }   - }Wuyi          the //{name= xiaoming, age=10} map data structure grow like this - //{"name": "Xiaoming", "Age": "Ten"} JSONOBJECT data structure has grown like this

————————————————————————

From Map-json Object-JSON character 1 public class a11111111 {

2       Public Static voidMain (string[] args) {3Map map =NewHashMap ();4Map.put ("msg", "yes");//map contains Yes5Jsonobject Jsonobject =jsonobject.fromobject (map);
and reuse the same piece of code,6SYSTEM.OUT.PRINTLN ("The result of the output is:" +jsonobject);7 //3. Convert the JSON object to a JSON string8String result =jsonobject.tostring ();9 System.out.println (result);Ten } One /*The result of the output is: {"msg": "Yes"}*/ A //{"MSG": "Yes"} long: But the above is the data structure (Jsonobject), the following is the string - //data Structures in Java can be output directly by name, or data structure names. ToString Output
The top one can call a lot of methods--the next one is just a string, not so many ways.
-}

Jsonobject Jsonobject = Jsonobject.fromobject

Map and Jsonobject the 2 data structures between the transformations

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.