初识Json

来源:互联网 发布:go并发编程实战 微盘 编辑:程序博客网 时间:2024/05/15 15:36

Json:javascript对象表示法。

Json是存储和文本信息的语法。

Json是轻量级的文本交换数据格式。

Json独立于语言和平台。

Json具有自我描述性,更容易理解。

类似于XML,但是比XML更小、更快、更易解析。

没有结束标签

更短

读写的速度更快

使用数组

不适用保留字

Json语法是javascript对象表示语法的子集。

数据在名称/值 对中

数据由逗号分隔

花括号保存对象

方括号保存数组

Json值可以是:数字(证书或浮点数)、字符串(在双引号中)、逻辑值(true或false)、数组(在方括号中)、对象(在花括号中)、null。

eg:{"firstName":"Jack","lastName":"Kite"}

try {
InputStreamReader isr = new InputStreamReader(getAssets().open(
"list_1.json"), "UTF-8");
BufferedReader br = new BufferedReader(isr);
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
isr.close();
JSONObject jo = new JSONObject();
System.out.println("countcommenturl"
+ jo.getString("countcommenturl"));
JSONArray ja = jo.getJSONArray("news");
for (int i = 0; i < ja.length(); i++) {
JSONObject js = ja.getJSONObject(i);
System.out.println("-------------------");
System.out.println("id" + js.getString("id"));
System.out.println("title " + js.getString("title"));
System.out.println("url" + js.getString("url"));
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}

0 0
原创粉丝点击