Java Background JSON operation

Source: Internet
Author: User

There are two ways to convert a parameter to a JSON-formatted string:
Way One:
Jsonobject Jo = new Jsonobject ();
Jo.put ("Certno", "522121198710114617");
Jo.put ("Certtype", "Identity_card");
String jostr = jo.tojsonstring ();

Way two:
map<string, object> map2 = new hashmap<string, object> ();
Map2.put ("Certno", "522121198710114617");
Map2.put ("Certtype", "Identity_card");
String jostr2 = jsonobject.tojsonstring (MAP2);

To convert a JSON format string to map:
/**

    • Parse the jsonstring into map<string,object>>
    • @param jsonstring
    • @return
      */
      public static map<string, object> Getmap (String jsonstring) {
      map<string, object> map = new hashmap<string, object> ();
      try {
      Map = Json.parseobject (Jsonstring,map.class);
      } catch (Exception e) {
      E.printstacktrace ();
      }
      return map;
      }

To take the desired parameter from the JSON object:
Object o = Json.tojson (response);
String str = o.tostring ();
Jsonobject Jo = Jsonobject.parseobject (str);
Jsonobject params = jo.getjsonobject ("params");

Java Background JSON operation

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.