JSON解析(1)

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值