Java解析json中不同的数据类型

废话不多说,直接上货

  • 直接解析

  1. 一个JSON对象——JSONObject
    {"name":"小明" , "age":20 , "male":true}

    public void parseJsonObject(String json) {  
            try {  
                JSONObject jsonObject = new JSONObject(json);  
                String name = jsonObject.getString("name");  
                int age = jsonObject.getInt("age");  
                boolean male = jsonObject.getBoolean("male");  
                //接下来该干嘛干嘛  
            } catch (JSONException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
        }
  2. 一个JSON数组——JSONArray
    [{"name":"胡小威" , "age":20 , "male":true},{"name":"赵小亮" , "age":22 , "male":false}]

    public void parseJSONArray(String json) {  
            try {  
                JSONArray jsonArray = new JSONArray(json);  
                for (int i = 0; i < jsonArray.length(); i++) {  
                    jsonObject = jsonArray.getJSONObject(i);  
                    String name = jsonObject.getString("name");  
                    int age = jsonObject.getInt("age");  
                    boolean male = jsonObject.getBoolean("male");  
                    //接下来该干嘛干嘛  
                }  
            } catch (JSONException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
        }
  3. 复杂一点的JSONObject
    {"name":"胡小威", "age"=20, "male":true, "address":{"street":"岳麓山南", "city":"长沙","country":"中国"}}

    public void parseJsonPerson(String json) {  
            try {  
                JSONObject jsonObject = new JSONObject(json);  
                String name = jsonObject.getString("name");  
                int age = jsonObject.getInt("age");  
                boolean male = jsonObject.getBoolean("male");  
                JSONObject addressJSON = jsonObject.getJSONObject("address");  
                String street = addressJSON.getString("street");  
                String city = addressJSON.getString("city");  
                String country = addressJSON.getString("country");  
                Address address = new Address(street, city, country);  
                Person person = new Person(name, age, male, address);  
            } catch (JSONException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
        }
  4. 复杂一点的JSONArray
    [

    {"name":"胡小威", "age"=20, "male":true, "address":{"street":"岳麓山南", "city":"长沙","country":"中国"}},

    {"name":"赵小亮", "age"=22, "male":false, "address":{"street":"九州港", "city":"珠海","country":"中国"}}

    ]

    public void parseJsonArrayPerson(String json) {  
            List<Person> persons = new ArrayList<Person>();  
            try {  
                jsonArray = new JSONArray(json);  
                for (int i = 0; i < jsonArray.length(); i++) {  
                    jsonObject = jsonArray.getJSONObject(i);  
                    String name = jsonObject.getString("name");  
                    int age = jsonObject.getInt("age");  
                    boolean male = jsonObject.getBoolean("male");  
                    JSONObject addressJSON = jsonObject.getJSONObject("address");  
                    String street = addressJSON.getString("street");  
                    String city = addressJSON.getString("city");  
                    String country = addressJSON.getString("country");  
                    Address address = new Address(street, city, country);  
                    Person person = new Person(name, age, male, address);  
                    persons.add(person);  
                    Log.v("juno", person.toString());  
                }  
            } catch (JSONException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
            Log.v("juno", persons.toString());  
        }

猜你喜欢

转载自blog.csdn.net/qq_43550109/article/details/90035135