public class MainActivity extends AppCompatActivity {
private TextView tv;
private Button bt;
private String readyJson = "[{\"a\":\"123\",\"b\":\"456\",\"F\":{\"aa\":\"111\",\"bb\":\"222\"}},{\"a\":\"321\",\"b\":\"654\",\"F\":{\"aa\":\"333\",\"bb\":\"444\"}}]";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = findViewById(R.id.tv);
bt = findViewById(R.id.start);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
parseJsonWithJSONObject(readyJson);
}
});
}
private void parseJsonWithJSONObject(String readyJson) {
try {
JSONArray jsonArray = new JSONArray(readyJson);
for (int i = 0; i<jsonArray.length();i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
Log.d("MainActivity",jsonObject.getString("a")+"+++++++++++++");
tv.append(jsonObject.getString("a")+" ");
Log.d("MainActivity",jsonObject.getString("b")+"-------------");
tv.append(jsonObject.getString("b")+" ");
//此处开始解析嵌套的json
JSONObject jsonObject1 = new JSONObject(jsonObject.getString("F"));
tv.append(jsonObject1.getString("aa")+" ");
tv.append(jsonObject1.getString("bb")+" ");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
利用JSONObject解析带有嵌套的json数组
最新推荐文章于 2024-09-29 23:59:08 发布