一.Json字符串转换成JavaBean
1.数据源
{"result":[{"sex":"男","age":"20","name":"张三"},{"sex":"女","age":"22","name":"小华"},{"sex":"男","age":"25","name":"李四"},{"sex":"女","age":"27","name":"小丽"}] ,"school":"清华大学","error":false}
2.代码
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private String data = "{\"result\":[{\"sex\":\"男\",\"age\":\"20\",\"name\":\"张三\"},{\"sex\":\"女\",\"age\":\"22\",\"name\":\"小华\"},{\"sex\":\"男\",\"age\":\"25\",\"name\":\"李四\"},{\"sex\":\"女\",\"age\":\"27\",\"name\":\"小丽\"}] ,\"school\":\"清华大学\",\"error\":false}";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
testMethod();
}
private void testMethod() {
List<Student> list = new ArrayList<Student>();
try {
JSONObject jsonObject = new JSONObject(data);
//获取Object类型数据
String school = jsonObject.getString("school");
boolean error = jsonObject.getBoolean("error");
Log.d("TAG", "school----:" + school);
Log.d("TAG", "error----:" + error);
//获取Array类型数据
JSONArray jsonArray = jsonObject.getJSONArray("result");
int length = jsonArray.length();
for (int i = 0; i < length; i++) {
Student student = new Student();
JSONObject object = (JSONObject) jsonArray.opt(i);
student.setName(object.getString("name"));
student.setAge(object.getString("age"));
student.setSex(object.getString("sex"));
list.add(student);
}
} catch (JSONException e) {
e.printStackTrace();
}
//遍历集合
Iterator<Student> iterator = list.iterator();
while (iterator.hasNext()) {
Student student = iterator.next();
String name = student.getName();
String age = student.getAge();
String sex = student.getSex();
Log.d("TAG", "name----:" + name);
Log.d("TAG", "age----:" + age);
Log.d("TAG", "sex----:" + sex);
}
}
}
3.结果
D/TAG: school----:清华大学
D/TAG: error----:false
D/TAG: name----:张三
D/TAG: age----:20
D/TAG: sex----:男
D/TAG: name----:小华
D/TAG: age----:22
D/TAG: sex----:女
D/TAG: name----:李四
D/TAG: age----:25
D/TAG: sex----:男
D/TAG: name----:小丽
D/TAG: age----:27
D/TAG: sex----:女
如果解析的Json格式,有些字段有时候会返回,有时候不会返回。可以使用optXXX来解析。
1.代码
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private String data = "{\"result\":[{\"sex\":\"男\",\"age\":\"20\",\"name\":\"张三\"},{\"sex\":\"女\",\"age\":\"22\",\"name\":\"小华\"},{\"sex\":\"男\",\"age\":\"25\",\"name\":\"李四\"},{\"sex\":\"女\",\"age\":\"27\",\"name\":\"小丽\"}] ,\"school\":\"清华大学\",\"error\":false}";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
testMethod();
}
private void testMethod() {
List<Student> list = new ArrayList<Student>();
try {
JSONObject jsonObject = new JSONObject(data);
//获取Object类型数据
String school = jsonObject.optString("school");
boolean error = jsonObject.optBoolean("error");
Log.d("TAG", "optString获取school----:" + school);
Log.d("TAG", "optBoolean获取error----:" + error);
//获取Array类型数据
JSONArray jsonArray = jsonObject.getJSONArray("result");
int length = jsonArray.length();
for (int i = 0; i < length; i++) {
Student student = new Student();
JSONObject object = (JSONObject) jsonArray.opt(i);
student.setName(object.optString("name"));
student.setAge(object.optString("age"));
student.setSex(object.optString("sex"));
list.add(student);
}
} catch (JSONException e) {
e.printStackTrace();
}
//遍历集合
Iterator<Student> iterator = list.iterator();
while (iterator.hasNext()) {
Student student = iterator.next();
String name = student.getName();
String age = student.getAge();
String sex = student.getSex();
Log.d("TAG", "optString获取name----:" + name);
Log.d("TAG", "optString获取age----:" + age);
Log.d("TAG", "optString获取sex----:" + sex);
}
}
}
2.结果
D/TAG: optString获取school----:清华大学
D/TAG: optBoolean获取error----:false
D/TAG: optString获取name----:张三
D/TAG: optString获取age----:20
D/TAG: optString获取sex----:男
D/TAG: optString获取name----:小华
D/TAG: optString获取age----:22
D/TAG: optString获取sex----:女
D/TAG: optString获取name----:李四
D/TAG: optString获取age----:25
D/TAG: optString获取sex----:男
D/TAG: optString获取name----:小丽
D/TAG: optString获取age----:27
D/TAG: optString获取sex----:女
如果数据源格式如下
{"result": ["列表1","列表2","列表3","列表4"]}
1.代码
package com.example.myapplication;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
private String data = "{\"result\": [\"列表1\",\"列表2\",\"列表3\",\"列表4\"]}";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
testMethod();
}
private void testMethod() {
try {
JSONObject jsonObject = new JSONObject(data);
JSONArray jsonArray = jsonObject.getJSONArray("result");
int length = jsonArray.length();
for (int i = 0; i < length; i++) {
String result = jsonArray.getString(i);
Log.d("TAG", "result----:" + result);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
2.结果
D/TAG: result----:列表1
D/TAG: result----:列表2
D/TAG: result----:列表3
D/TAG: result----:列表4
3.附
即JSONArray可以用下标直接获取String,Int,Long,Double等类型。
二.JavaBean转换成Json字符串
1.代码
package com.example.myapplication;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
testMethod();
}
private void testMethod() {
Student student1 = new Student();
student1.setAge("20");
student1.setName("张三");
student1.setSex("男");
Student student2 = new Student();
student2.setAge("22");
student2.setName("小华");
student2.setSex("女");
Student student3 = new Student();
student3.setAge("25");
student3.setName("李四");
student3.setSex("男");
Student student4 = new Student();
student4.setAge("27");
student4.setName("小丽");
student4.setSex("女");
List<Student> list = new ArrayList<Student>();
list.add(student1);
list.add(student2);
list.add(student3);
list.add(student4);
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = new JSONArray();
int length = list.size();
for (int i = 0; i < length; i++) {
Student student = list.get(i);
JSONObject object = new JSONObject();
try {
object.put("name", student.getName());
object.put("age", student.getAge());
object.put("sex", student.getSex());
jsonArray.put(i, object);
} catch (JSONException e) {
e.printStackTrace();
}
}
try {
jsonObject.put("result", jsonArray);
jsonObject.put("error", false);
jsonObject.put("school", "清华大学");
} catch (JSONException e) {
e.printStackTrace();
}
String result = jsonObject.toString();
Log.d("TAG", "result----:" + result);
}
}
2.结果
D/TAG: result----:{"result":[{"name":"张三","age":"20","sex":"男"},{"name":"小华","age":"22","sex":"女"},{"name":"李四","age":"25","sex":"男"},{"name":"小丽","age":"27","sex":"女"}],"error":false,"school":"清华大学"}