以下のコードでは、JSON 配列に JSON オブジェクト (本とカメラ) を入力しようとしています。
[ {
"camera":{
"picture":"http:\/\/img7.flixcart.com\/image\-imacyb5emj5yztet.jpeg",
"model":"Lumix DMC-FP3 Point & Shoot",
"make":"Panasonic",
"price":5830
}
},
{
"camera":{
"picture":"http:\/\/sp.sony-europe.com\/da.jpeg",
"model":"Digital Still Camera - H Series - DSC-HX200V",
"make":"Sony",
"price":510
}
},
{
"book":{
"description":"What is self, and how can a",
"authors":"Douglas R Hofstadter",
"price":650,
"id":40724766,
"title":"G\u00f6del, Escher, Bach: an Eternal Golden Braid"
}
},
{
"camera":{
"picture":"http:\/\/www.usa.canon.com\/60d_586x186.gif",
"model":"Digital SLR Camera EOS 60D",
"make":"Canon",
"price":999
}
},
{
"book":{
"description":"Tgdfgf fgfg ",
"authors":"Harold Abelson and Gerald Jay Sussman with Julie Sussman",
"price":469,
"id":51087,
"title":"Structure and Interpretation of Computer Programs"
}
},
]
public void getDetalFromServer() {
// TODO Auto-generated method stub
String URL="xyz.php";
HttpClient client= new DefaultHttpClient();
HttpPost post=new HttpPost(URL);
try {
HttpResponse response =client.execute(post);
HttpEntity getResEntity=response.getEntity();
String result="";
if(getResEntity!=null){
result=EntityUtils.toString(getResEntity);
System.out.println("result from server: "+result);
bookDescription=new ArrayList<String>();
bookAuthors=new ArrayList<String>();
bookPrice=new ArrayList<String>();
bookID=new ArrayList<String>();
bookTitle=new ArrayList<String>();
responseArray=new JSONArray(result);
for(int i=0;i<responseArray.length();i++){
JSONObject bookObject=(JSONObject) responseArray.getJSONObject(i);
bookDescription.add(bookObject.get("description").toString());
bookAuthors.add(bookObject.get("authors").toString()); bookPrice.add(bookObject.get("price").toString());
bookID.add(bookObject.get("id").toString()); bookTitle.add(bookObject.get("タイトル").toString());
}
}
} catch (Exception e) {
// TODO: handle exception
}
}
このコードを使用すると、応答の最初のオブジェクトを取得できます。今、応答から Music オブジェクトを解析しようとしています。