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"}]