1:用系统自带的JSONArray和JSONObject进行解析
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder()
.url("http://192.168.43.24:11403/sss/he.json")
.build();
try {
Response response = okHttpClient.newCall(request).execute();
String responsedata = response.body().string();
ParseJson(responsedata);
} catch (Exception e) {
e.printStackTrace();
}
}
}
).start();
}
});
}
private void ParseJson(String responsedata) throws Exception {
JSONArray jsonArray=new JSONArray(responsedata);
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonObject=jsonArray.getJSONObject(i);
String id =jsonObject.getString("id");
String name=jsonObject.getString("name");
String version=jsonObject.getString("version");
Log.i("xinxi","id为:"+id);
Log.i("xinxi","name为:"+name);
Log.i("xinxi","version为:"+version);
}
}
}
/*tomcat上的数据为:
[{"id":"5","version":"5.0","name":"qq"},{"id":"6","version":"6.0","name":"yy"},{"id":"7","version":"7.0","name":"mm"}]
经过解析,输出结果为:
11-16 17:25:07.436 5739-6324/com.example.liang.myapplication I/xinxi: id为:5
11-16 17:25:07.436 5739-6324/com.example.liang.myapplication I/xinxi: name为:qq
11-16 17:25:07.436 5739-6324/com.example.liang.myapplication I/xinxi: version为:5.0
11-16 17:25:07.436 5739-6324/com.example.liang.myapplication I/xinxi: id为:6
11-16 17:25:07.436 5739-6324/com.example.liang.myapplication I/xinxi: name为:yy
11-16 17:25:07.436 5739-6324/com.example.liang.myapplication I/xinxi: version为:6.0
11-16 17:25:07.436 5739-6324/com.example.liang.myapplication I/xinxi: id为:7
11-16 17:25:07.436 5739-6324/com.example.liang.myapplication I/xinxi: name为:mm
11-16 17:25:07.436 5739-6324/com.example.liang.myapplication I/xinxi: version为:7.0*/
2:用gson进行解析(gson的具体使用方法)
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder()
.url("http://192.168.43.24:11403/sss/he.json")
.build();
try {
Response response = okHttpClient.newCall(request).execute();
String responsedata = response.body().string();
ParseJson(responsedata);
} catch (Exception e) {
e.printStackTrace();
}
}
}
).start();
}
});
}
private void ParseJson(String responsedata) throws Exception {
Gson gson=new Gson();
//gson.fromJson(responsedata,shuju.class);//这条代码只能解析一个shuju对象
List<shuju> list=gson.fromJson(responsedata,new TypeToken<List<shuju>>(){}.getType());//这条代码可以解析json数组,生成多个shuju对象
for(shuju shuju:list){
Log.i("xinxi",shuju.toString());
}
}
}
/*tomcat上的数据为:
[{"id":"5","version":"5.0","name":"qq"},{"id":"6","version":"6.0","name":"yy"},{"id":"7","version":"7.0","name":"mm"}]
经过解析,输出结果为:
11-16 17:41:30.006 23465-23576/com.example.liang.myapplication I/xinxi: shuju{id='5', name='qq', version='5.0'}
11-16 17:41:30.006 23465-23576/com.example.liang.myapplication I/xinxi: shuju{id='6', name='yy', version='6.0'}
11-16 17:41:30.006 23465-23576/com.example.liang.myapplication I/xinxi: shuju{id='7', name='mm', version='7.0'}*/
在shuju.java文件中
public class shuju {
private String id;
private String name;
private String version;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
@Override
public String toString() {
return "shuju{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", version='" + version + '\'' +
'}';
}
}