Jsonobject Traversal Org.json.JSONObject

Source: Internet
Author: User

[HTML]View PlainCopy
    1. Import Org.json.JSONArray;
    2. Import org.json.JSONException;
    3. Import Org.json.JSONObject;

[Java]View PlainCopy
  1. Public static void Main (string[] args) {
  2. String str = "{' TI ': [{' Value ': ' Aa1 ', ' count ': ' 10},{' value ': ' Aa2 ', ' count ': 15},{' value ': ' Aa3 ', ' Count ': 20}]," +
  3. "' ab ': [{' Value ': ' AB ', ' count ': 110},{' value ': ' ab2 ', ' count ': 115},{' value ': ' ab3 ', ' Count ': 210}]}";
  4. Jsonarray NewArray = new Jsonarray ();
  5. Jsonobject Newjson = new Jsonobject ();
  6. try {
  7. Jsonobject obj = new Jsonobject (str);
  8. Iterator it = Obj.keys ();
  9. While (It.hasnext ()) {
  10. String key = (string) it.next ();
  11. String value = obj.getstring (key);
  12. Jsonarray array = Obj.getjsonarray (key);
  13. For (int i=0;i<array.length (); i++) {
  14. Jsonobject Jsonobject = Array.getjsonobject (i);
  15. Jsonobject.put ("name", key);
  16. Jsonobject.put ("exp", key+"=" +jsonobject.getstring ("value"));
  17. Newarray.put (Jsonobject);
  18. }
  19. }
  20. Newjson.put ("groups", NewArray);
  21. System.out.println (Newjson);
  22. } catch (Jsonexception e) {
  23. E.printstacktrace ();
  24. }
  25. }
[Java]View PlainCopy
  1. {"Groups": [{"Exp":"Ab=ab","Count":110,"Name":"AB","Value":"AB"},{"Exp":"Ab=ab2","Count":115,"Name":"AB","Value":"AB2"},{"Exp":"Ab=ab3","Count":210, "name": "AB3"} , { 10, "name": " Aa1 "},{" exp ": " Ti=aa2 ", 15, "name": "value": Span class= "string" > "Aa2"},{ "exp": "Ti=aa3", 20," Value ": " Aa3 "}]}&NBSP;&NBSP;

Jsonobject Traversal Org.json.JSONObject

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.