JSON解析(1)

来源:互联网 发布:数据库管理dba 编辑:程序博客网 时间:2024/06/03 20:08

1.什么是JSON

JSON :  数据组织格式

xml:
<books>
   <book>
      <id>100</id>
      <name>Android编程</name>
   </book>
   <book>
        <id>101</id>
        <name>JAVA编程</name>
   </book>
</books>


2.JSON格式

  {"名称":值}
  {"名称":{"名称1":值1}}

  {"名称1":值1,"名称2":值2}

  {"名称1":[{"名称1":值1},{"名称1":值1}]}

  值类型:  String    int     jsonObject     array


  {"id":100}      {"name":"Android编程"}
  {"book1":{"id":100,"name":"Android编程"}}
  {"book2":{"id":101,"name":"JAVA编程"}}

  [{"book":{"id":100,"name":"Android编程"}},{"book":{"id":101,"name":"JAVA编程"}}]

  {"books":[{"book":{"id":100,"name":"Android编程"}},{"book":{"id":101,"name":"JAVA编程"}}]}

 

3.JSON解析:

public void testJson() {        String jsonStr = "{\"name\":\"Android编程\"}";        try {            JSONObject jsonObject = new JSONObject(jsonStr);            String bookName = jsonObject.getString("name");            Log.e("tag", "bookName  :" + bookName);        } catch (JSONException e) {            e.printStackTrace();        }    }    public void testJsonBook() {        String jsonStr = "{\"book1\":{\"id\":100,\"name\":\"Android编程\"}}";        try {            JSONObject jsonObject = new JSONObject(jsonStr);            JSONObject jsonObject1 = jsonObject.getJSONObject("book1");            String bookName = jsonObject1.getString("name");            int bookId = jsonObject1.getInt("id");            Log.e("tag", "bookName :" + bookName + ", bookId :" + bookId);        } catch (JSONException e) {            e.printStackTrace();        }    }    public void testJsonArray() {        String jsonStr = "[{\"book\":{\"id\":100,\"name\":\"Android编程\"}},{\"book\":{\"id\":101,\"name\":\"JAVA编程\"}}]";        try {            JSONArray jsonArray = new JSONArray(jsonStr);            int length = jsonArray.length();            for (int i = 0; i < length; i++) {                JSONObject jsonObject = jsonArray.getJSONObject(i);                JSONObject jsonBook = jsonObject.getJSONObject("book");                String bookName = jsonBook.getString("name");                int bookId = jsonBook.getInt("id");                Log.e("tag", "bookName :" + bookName + ", bookId :" + bookId);            }        } catch (JSONException e) {            e.printStackTrace();        }


从json文件中解析json。

{"books":[{"book":{"id":100,"name":"Android编程"}},{"book":{"id":101,"name":"JAVA编程"}}]}
    public void test() throws IOException {        AssetManager assetManager = getContext().getAssets();        InputStream inputStream = assetManager.open("book.json");        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));        String line = "";        StringBuilder stringBuilder = new StringBuilder();        while ((line = reader.readLine()) != null) {            stringBuilder.append(line);        }        String jsonStr = stringBuilder.toString();        try {            JSONObject jsonObject = new JSONObject(jsonStr);            String books = jsonObject.getString("books");            JSONArray jsonArray = new JSONArray(books);            int length = jsonArray.length();            for (int i = 0; i < length; i++) {                JSONObject jsonObject1 = jsonArray.getJSONObject(i);                JSONObject jsonBook = jsonObject1.getJSONObject("book");                int id = jsonBook.getInt("id");                String name = jsonBook.getString("name");                Log.e("tag", "books:book:id=" + id + "name=" + name);            }        } catch (JSONException e) {            e.printStackTrace();        }    }



 

0 0
原创粉丝点击