一.JSON的简介:
JSON建构于两种结构:
(1)“名称/值”对的集合(A collection of name/value pairs)。不同的语言中,它被理解为对象(object),纪录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组 (associative array)。
(2)值的有序列表(An ordered list of values)。在大部分语言中,它被理解为数组(array)。
这些都是常见的数据结构。事实上大部分现代计算机语言都以某种形式支持它们。这使得一种数据格式在同样基于这些结构的编程语言之间交换成为可能。
JSON具有以下这些形式:
对象是一个无序的“‘名称/值’对”集合。一个对象以“{”(左括号)开始,“}”(右括号)结束。每个“名称”后跟一个“:”(冒号);“‘名称/值’ 对”之间使用“,”(逗号)分隔。
数组是值(value)的有序集合。一个数组以“[”(左中括号)开始,“]”(右中括号)结束。值之间使用“,”(逗号)分隔。
值(value)可以是双引号括起来的字符串(string)、数值(number)、true
、false
、 null
、对象(object)或者数组(array)。这些结构可以嵌套。
字符串(string)是由双引号包围的任意数量Unicode字符的集合,使用反斜线转义。一个字符(character)即一个单独的字符串(character string)。
字符串(string)与C或者Java的字符串非常相似。
数值(number)也与C或者Java的数值非常相似。除去未曾使用的八进制与十六进制格式。除去一些编码细节。
二.ANDROID中JSON的API简介:
1.JSONObject,JSONArry,JSONStringer类构建json文件
2.getType可以将要获取的键的值转换为指定的类型,如果无法转换或没有值得时候抛出异常JSONRException
optType也可以将要获取的键的值转换为指定的类型,如果无法转换或没有值得时候返回用户提供或默认提供的值
三.Android中对于JSON的使用:
界面:
MainActivity的主要代码:
package com.example.creatjson;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private JSONObject mContactJson;
private File file = null;
private FileOutputStream fileOutputStream = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
Button btCreatJSON = (Button) findViewById(R.id.bt_write_json);
btCreatJSON.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ShowJSON.class);
intent.putExtra("JSON", creatJSON());
startActivity(intent);
}
});
Button btReadJSON = (Button) findViewById(R.id.bt_read_json);
btReadJSON.setOnClickListener(new OnClickListener() {
String line;
@Override
public void onClick(View v) {
try {
InputStreamReader inputStreamReader = new InputStreamReader(
getAssets().open("Json.json"), "UTF-8");
BufferedReader bufferedReader = new BufferedReader(
inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
inputStreamReader.close();
bufferedReader.close();
JSONObject mContactJson = new JSONObject(stringBuilder
.toString());
line = null;
line += "stranger:" + mContactJson.get("stranger") + "\n";
JSONArray jsonArray = mContactJson.getJSONArray("friend");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
line += "friendname:"
+ jsonObject.getString("friendname")
+ " number:" + jsonObject.getString("number")
+ "\n";
}
} catch (Exception e) {
e.printStackTrace();
}
Intent intent = new Intent(MainActivity.this, ShowJSON.class);
intent.putExtra("JSON", line);
startActivity(intent);
}
});
}
private String creatJSON() {
try {
mContactJson = new JSONObject();
JSONArray mFriend = new JSONArray();
JSONObject friend1 = new JSONObject();
friend1.put("friendname", "zhangsan");
friend1.put("number", "131456789");
JSONObject friend2 = new JSONObject();
friend2.put("friendname", "lisi");
friend2.put("number", "131456789");
JSONObject friend3 = new JSONObject();
friend3.put("friendname", "liuwu");
friend3.put("number", "131456789");
mFriend.put(friend1);
mFriend.put(friend2);
mFriend.put(friend3);
mContactJson.put("friend", mFriend);
mContactJson.put("stranger", "134651313");
} catch (JSONException e) {
e.printStackTrace();
}
return mContactJson.toString();
}
}
Android_JSON数据解析Demo: http://download.youkuaiyun.com/detail/two_water/9388361