1. 解析单个JSON对象
try
{
String JString="{\"Name\":\"George\",\"Age\":30,\"Birthday\":\"1982-3-17\"}";
JSONObject jsonObject = new JSONObject(JString);
String uid = jsonObject.getString("Name");
int a = jsonObject.getInt("Age");
String bir = jsonObject.getString("Birthday");
new AlertDialog.Builder(this).setTitle("Hint").setMessage(uid).setPositiveButton("OK", null).show();
new AlertDialog.Builder(this).setTitle("Hint").setMessage(String.valueOf(a)).setPositiveButton("OK", null).show();
new AlertDialog.Builder(this).setTitle("Hint").setMessage(bir).setPositiveButton("OK", null).show();
}
catch(Exception e)
{
e.printStackTrace();
}
2. 解析JSON数组
try
{
String JString="[{ \"id\": \"HAHA\", \"version\": \"abc\" },{ \"id\": \"BABA\", \"version\": \"bbc\" },{ \"id\": \"CACA\", \"version\": \"Wa_\" }]";
//JSONObject jsonObject = new JSONObject(JString);
JSONArray ja = new JSONArray(JString);
for(int i=0;i<ja.length();i++)
{
JSONObject jsonObject2 = (JSONObject)ja.get(i);
String uid = jsonObject2.getString("id");
new AlertDialog.Builder(this).setTitle("Hint").setMessage(uid).setPositiveButton("OK", null).show();
}
}
catch(Exception e)
{
e.printStackTrace();
}