Json解析
JavaScrip对象表示法(JavaScript Object Notation)JSON属于轻量级文本数据交换格式
JSon独立于平台和语言JSON具有自我描述性更易于理解
类似Xml,比Xml更小,更快,更易解析
- Json文件
数据保存在{key: value}(键值对)中
{“info”:{“name”:”jack”,”age:”20,”salary”:3000,”爱好”:[“看电影”,”打代码”,”跑步”]}}
- Json语法
Key数据类型:
String
Value数据类型:
{} JsonObject 类型
[] 数组类型
String 字符串类型
Int,float 数值类型
- Json解析相关类
JsonObject
Json对象,里面包含一个或多个键值对
JSONArray
Json数组,内部元素由链表管理
//取出json属猪中下标为index的元素
jsonArray.getString(index);
//Json数组中的元素可以为任意类型
JsonArray。getInt(index);
JsonArray.getJSONObject(index);
JsonArray.getJSONArray(index);
例子
MainActivity.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.button1) {
// 解析Json
readJson("info.json");
} else if (v.getId() == R.id.button2) {
// 打包json
// {"info": {"name":"think in java", "price":100,"author":["Jack","Lily"]}, "number":50}
closeJson();
}
}
private void closeJson(){
JSONObject obj=new JSONObject();
try {
obj.put("number", 50);
JSONObject infoobj=new JSONObject();
infoobj.put("name", "thingk in java");
infoobj.put("price", 100);
JSONArray array=new JSONArray();
array.put(0, "Jack");
array.put(1, "Lily");
obj.put("author", array);
obj.put("info", infoobj);
Log.d("Tag", ""+obj);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void readJson(String string) {
try {
InputStream is = this.getResources().getAssets().open(string);
InputStreamReader isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
String json = br.readLine();
Log.d("Tag", json);
//创建json对象
JSONObject obj = new JSONObject(json);
//key为info的内容
JSONObject infoObj=obj.getJSONObject("info");
//取出名字
String name=infoObj.getString("name");
//取出年龄
int age=infoObj.getInt("age");
//取出收入
int salary = infoObj.getInt("salary");
Log.d("Tag", "name = "+name+",age = "+age+",+salary = "+salary);
JSONArray array=infoObj.getJSONArray("favor");
for (int i = 0; i < array.length(); i++) {
String favor = array.getString(i);
Log.d("Tag", "favor = "+favor);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.android_830_json.MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="103dp"
android:text="解析Json" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="100dp"
android:text="打包Json" />
</RelativeLayout>