Android programming experience-JSON usage experience

Source: Internet
Author: User

Note the following issues when using JSON:


[Java] JSONObject jsonObject = new JSONObject ();
Try {
JsonObject. put ("test", "test 1 ");
JsonObject. put ("test", 100 );
} Catch (JSONException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
System. out. println (jsonObject );

JSONObject jsonObject = new JSONObject ();
Try {
JsonObject. put ("test", "test 1 ");
JsonObject. put ("test", 100 );
} Catch (JSONException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
System. out. println (jsonObject );
}

 

I thought it would output two column values for the first time. Later I found that the Json object is an unordered set of Name Value pairs (I .e. sub-elements), which is equivalent to a Map object, so the result is

{"Test": 100}

 

[Java] JSONObject jsonObject = new JSONObject ();
JSONArray member = new JSONArray ();
JSONObject jsonObject2 = new JSONObject ();
Try {
JsonObject. put ("test", "test 1 ");
JsonObject. put ("test1", 100 );
 
JsonObject2.put ("test", "test 1 ");
JsonObject2.put ("test1", 100 );
 
Member. put (jsonObject );
Member. put (jsonObject2 );
} Catch (JSONException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
 
System. out. println (member );

JSONObject jsonObject = new JSONObject ();
JSONArray member = new JSONArray ();
JSONObject jsonObject2 = new JSONObject ();
Try {
JsonObject. put ("test", "test 1 ");
JsonObject. put ("test1", 100 );

JsonObject2.put ("test", "test 1 ");
JsonObject2.put ("test1", 100 );
 
Member. put (jsonObject );
Member. put (jsonObject2 );
} Catch (JSONException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}

System. out. println (member );
}
JsonArray is different from jsonObject. It can store duplicate data and be ordered. Therefore, the output here is

[{"Test1": 100, "test": "test 1" },{ "test1": 100, "test": "test 1"}]

 

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.