2012-07-23 352 views
0

我遇到問題。我的應用程序不會從我的Web服務加載資源,因爲它的格式已更改。將JSON對象轉換爲JSON數組

我的web服務之前返回一個JSON數組是這樣的:

[{"productid":2,"sku":"SKU-2","productname":"SMART NEW","productdesc":"description","category":"default category","price":2000.1,"picture":"http://10.23.20.50:8080/msos-ws/images/products/smart.gif","remarks":"remarks"}] 

現在,它返回一個JSON對象我想:

{"product":[{"category":"default category","picture":"http://localhost:8080/msos-ws/images/products/smart.gif","price":"480.0","productdesc":"Dragon Flash Disk (Special)","productid":"7","productname":"Kingston 8GB","remarks":"remarks","sku":"SKU-7"}]} 

這裏是我的代碼在後端:

@GET 
@Override 
@Produces("application/json") 
public List<Product> findAll() { 
     return prodManager.getProducts(); 
} 

我返回一個列表,所以它應該在數組中,對嗎?

回答

0

你得到的是一個鍵/值對,其中關鍵是

product 

和1-元件陣列

[{"category":"default category","picture":"http://localhost:8080/msos-ws/images/products/smart.gif","price":"480.0","productdesc":"Dragon Flash Disk (Special)","productid":"7","productname":"Kingston 8GB","remarks":"remarks","sku":"SKU-7"}] 

指www.json.org/javadoc/org/json /JSONObject.html,看看下面的方法:

public java.lang.Object get(java.lang.String key) 

public JSONObject put(java.lang.String key, 
        java.util.Collection value) 

修改你的應用程序加載資源應該很容易n ow :)