Transfer: https://www.cnblogs.com/kkxwze/p/11134846.html
the first method:
String str = "{ 'array' : [{ 'id': 5, 'name': ' John Doe'}, { 'id': 6, 'name': ' John Doe'}]}"; the JSONArray JSONArray = null; JSONArray = jsonobj.getJSONArray ( "array"); // get the array System.out.println (jsonArray.getJSONObject (0) .get ( "name"));
String str = "[{'columnId':5,'columnName':'人文历史'},{'columnId':2,'columnName':'商业视野'}]}"; JSONArray jsonArray = null; jsonArray = new JSONArray(str); System.out.println(jsonArray.getJSONObject(0).get("columnName"));
Two, JAVA get all the key-value pairs in the json
JSONObject json1=JSONObject.fromObject("{'username' : '11111','clientid' : '','password' : '222222'}"); Map<String, Object> map =json1; for (Entry<String, Object> entry : map.entrySet()) { System.out.println(entry.getKey()+"="+entry.getValue()); }
All the values of an array of three extraction json
public class JsonExtracter {public static void main (String [] args) {String s = "{\" name \ ": \" a \ ", \" family \ ": [\" John Doe \ ", \" John Doe \ "]}"; the jSONObject jsonObject = JSON.parseObject (S); // Note: the contents of the family with brackets [], the object to be transformed JSONArray type JSONArray family = jsonObject.getJSONArray ( "family" ) ; for (int I = 0; I <family.size (); I ++) {// extract all family of String S1 = (String) family.get (I); System.out.println ( "currentFamily:" S1 +); }}
the first method:
String str = "{ 'array' : [{ 'id': 5, 'name': ' John Doe'}, { 'id': 6, 'name': ' John Doe'}]}"; the JSONArray JSONArray = null; JSONArray = jsonobj.getJSONArray ( "array"); // get the array System.out.println (jsonArray.getJSONObject (0) .get ( "name"));
String str = "[{'columnId':5,'columnName':'人文历史'},{'columnId':2,'columnName':'商业视野'}]}"; JSONArray jsonArray = null; jsonArray = new JSONArray(str); System.out.println(jsonArray.getJSONObject(0).get("columnName"));
Two, JAVA get all the key-value pairs in the json
JSONObject json1=JSONObject.fromObject("{'username' : '11111','clientid' : '','password' : '222222'}"); Map<String, Object> map =json1; for (Entry<String, Object> entry : map.entrySet()) { System.out.println(entry.getKey()+"="+entry.getValue()); }
All the values of an array of three extraction json
public class JsonExtracter {public static void main (String [] args) {String s = "{\" name \ ": \" a \ ", \" family \ ": [\" John Doe \ ", \" John Doe \ "]}"; the jSONObject jsonObject = JSON.parseObject (S); // Note: the contents of the family with brackets [], the object to be transformed JSONArray type JSONArray family = jsonObject.getJSONArray ( "family" ) ; for (int I = 0; I <family.size (); I ++) {// extract all family of String S1 = (String) family.get (I); System.out.println ( "currentFamily:" S1 +); }}