今天分享一篇Android中原生的json操作
sky-mxc 总结 转载注明原创:http://blog.csdn.net/mxiaochao?viewmode=contents
Android原生json操作 是通过JSONObject 和JSONArray两个对象操作的
- 解析json字符串
- 对象转为json字符串
解析json字符串
json字符串
{
"name":"张三",
"age":19,
"id":0,
"live":true,
"interest":["basketball","computer","programmer"]
}
实体类
/**
* Created by sky-mxc
*/
public class Student {
private String name;
private int id;
private int age;
private boolean live;
private String[] interests;
}
使用JSONObject 解析
/**
* 解析 json字符串
*/
private void parserJsonStr() {
try {
//得到json字符串
String json = getJsonStr(getAssets().open("object.json"));
//封装为json对象
JSONObject jo = new JSONObject(json);
//逐一获取属性
String name = jo.getString("name");
int age = jo.getInt("age");
int id = jo.getInt("id");
boolean live = jo.getBoolean("live");
JSONArray ja = jo.getJSONArray("interest");
String [] interest = new String[ja.length()];
for (int i=0;i<ja.length();i++){
interest[i]= ja.getString(i)+",";
}
Student stu = new Student(name,id,age,live,interest);
Log.e("Tag","===="+stu.toString());
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
/**
* 读取json字符串
* @param is 输入流
* @return json字符串
* @throws IOException
*/
public String getJsonStr(InputStream is) throws IOException {
StringBuffer json = new StringBuffer();
InputStreamReader isr = new InputStreamReader(is,"utf-8");
BufferedReader reader = new BufferedReader(isr);
String line =null;
while ((line = reader.readLine())!=null){
json.append(line);
}
return json.toString();
}
将对象转为json字符串
实体类
/**
* Created by sky-mxc
*/
public class Clazz {
private String name;
private List<Student> students;
}
转换
/**
* 将list集合转为json字符串
*/
private void listToJson() {
Clazz clazz = new Clazz();
clazz.setName("sky");
List<Student> stus = new ArrayList<>();
Student stu1 = new Student("张三",1,19,true,new String[]{"song","sport","swimming"});
Student stu2 = new Student("李四",1,20,true,new String[]{"song","sleep","swimming"});
Student stu3 = new Student("王五",1,39,false,new String[]{"drink","sport","swimming"});
stus.add(stu1);
stus.add(stu2);
stus.add(stu3);
clazz.setStudents(stus);
//转为json字符串
JSONObject jo = new JSONObject();
try {
jo.put("name",clazz.getName());
JSONArray ja = new JSONArray();
for (Student stu:stus){
JSONObject j = new JSONObject();
j.put("name",stu.getName());
j.put("age",stu.getAge());
j.put("live",stu.isLive());
JSONArray jsonArray = new JSONArray();
for(int i=0;i<stu.getInterests().length;i++){
jsonArray.put(stu.getInterests()[i]);
}
j.put("interest",jsonArray);
ja.put(j);
}
jo.put("students",ja);
} catch (JSONException e) {
e.printStackTrace();
}
Log.e("Tag","=list ot json ="+jo.toString());
}
结果:
{
"name":"sky",
"students":[
{
"name":"张三",
"age":19,
"live":true,
"interest":[
"song",
"sport",
"swimming"
]
},
{
"name":"李四",
"age":20,
"live":true,
"interest":[
"song",
"sleep",
"swimming"
]
},
{
"name":"王五",
"age":39,
"live":false,
"interest":[
"drink",
"sport",
"swimming"
]
}
]
}
原生json操作真的好繁琐 。我还是使用的fastjson较多一点
推荐一个json检查的网站 特别好用 http://json.cn/