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
- json解析:[1]gson解析json
- json解析:[1]gson解析json
- json 解析1
- JSON解析(1)
- 【JSON解析】JSON解析
- 最简单Json解析1
- json: RapidJson 解析(1)
- json 解析之Gson (1)
- Android JSON解析(1)
- iOS json数据解析(1)
- luaError json解析错误1
- 解析JSON
- Json解析
- JSON解析
- JSON解析
- json解析
- Json解析
- Json解析
- Redis源码解析:01简单动态字符串SDS
- jsp生成二维码
- Django根据现有数据库建立model
- O(N)时间复杂度的排序算法-计数排序
- Brocade SAN Switch CFG backup and recover
- JSON解析(1)
- 【Hibernate系列】(一):心底好似旧相识
- 素材类网站 (欢迎补充)
- Java 多态详解 总结 以及 fina,abstractl关键字详解
- Android测试之Monkey
- 单向循环链表
- jQuery中的事件
- Linux中线程和进程的区别
- 深入分析JavaWeb Item40 -- 文件上传和下载