2013-03-13 80 views
0
{ 
getJobDetailsResult: [ 
{ 
    Client: 
    { 
    ClientID: 265, 
    ClientName: "RETRAVISION (WA) LTD" 
    }, 
    ConsignmentActive: true, 
    ConsignmentCreationDate: "6/06/2012", 
    ConsignmentCustRef: "855929", 
    ConsignmentID: 304005, 
    OrderNo: "20807354", 
    ShipTo: 
    { 
    ShipToAddress1: "c/o HELENWAY ", 
    ShipToAddress2: "WHS E4 UNIT 12 MARKET CITY ", 
    ShipToCity: "CANNINGVALE", 
    ShipToContactName: null, 
    ShipToId: 18933, 
    ShipToName: "DALWALLINU RETRAVISION 2", 
    ShipToPCode: "6155", 
    ShipToState: "WA" 
    } 
} 
] 
} 

嗨,大家好,我目前正試圖使用​​Android檢索JSON,到目前爲止,我沒有遇到任何問題。但是,我有一個小問題,那就是檢索Consignment類引用的對象,即ShipTo和Client類。從Android中檢索JSON中的引用對象

到目前爲止,我能夠檢索寄存值,如ConsignmentActive,ConsignmentCreationDate,ConsignmentCustRef,ConsignmentID和OrderNo。但是,我不確定如何映射ShipTo和客戶端中的項目。

這是我當前的代碼,而我試圖在一個方法來執行: -

public Consignments getConsignmentManifest(String consignment) 
{ 
    Consignments con = new Consignments(); 

    try 
    { 
     DefaultHttpClient client = new DefaultHttpClient(); 
     String theString = new String(""); 
     //http get request 
     HttpGet request = new HttpGet(POD_URI + "/getJobDetails/" + consignment); 
     //set the hedear to get the data in JSON format 
     request.setHeader("Accept", "application/json"); 
     request.setHeader("Content-type", "application/json"); 

     //get the response 
     HttpResponse response = client.execute(request); 
     HttpEntity entity = response.getEntity(); 
     InputStream is = entity.getContent(); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 

     StringBuilder builder = new StringBuilder(); 
     String line; 
     while ((line = reader.readLine()) != null) 
     { 
      builder.append(line); 
     } 
     is.close(); 

     theString = builder.toString(); 

     JSONObject conJSON = new JSONObject(theString); 
     JSONArray cons = conJSON.getJSONArray("getJobDetailsResult"); 

     for(int i = 0; i < cons.length(); i++) 
     { 
      JSONObject cObj = cons.getJSONObject(i); 
      con.ConsignmentID = cObj.getInt("ConsignmentID"); 
      con.ConsignmentCreationDate = cObj.getString("ConsignmentCreationDate"); 
      con.ConsignmentCustRef = cObj.getString("ConsignmentCustRef"); 
      con.OrderNo = cObj.getString("OrderNo"); 
      con.ConsignmentActive = cObj.getBoolean("ConsignmentActive"); 

     } 
    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 

    return con; 
} 

我很希望知道我可以從這裏獲取SHIPTO對象。我嘗試過這樣的東西,但我得到了一個例外: -

con.Clients.ClientID = con.getint("ClientID"); 

有人可以告訴我如何改善?所有的幫助將不勝感激。謝謝。

編輯:我已經找出了問題。這裏是代碼: -

//Client object 
       Clients cl = new Clients(); 
       cl.ClientId = clObj.getInt("ClientID"); 
       cl.ClientName = clObj.getString("ClientName"); 
       con.Clients = cl; 

       //ShipTo object 
       JSONObject stObj = cObj.getJSONObject("ShipTo"); 
       ShipTo sto = new ShipTo(); 
       sto.ShipToId = stObj.getInt("ShipToId"); 
       sto.ShipToName = stObj.getString("ShipToName"); 
       sto.ShipToAddress1 = stObj.getString("ShipToAddress1"); 
       sto.ShipToAddress2 = stObj.getString("ShipToAddress2"); 
       sto.ShipToCity = stObj.getString("ShipToCity"); 
       sto.ShipToPostcode = stObj.getString("ShipToPCode"); 
       sto.ShipToState = stObj.getString("ShipToState"); 
       con.ShipTo = sto; 

所有3個對象;我的託管,託運和客戶對象在Android中現在正在填充。

回答

0

首先解析這樣

String response = EntityUtils.toString(response.getEntity()); 

響應得到響應從這樣

JSONObject json = new JSONObject(response); 

響應創建JSON對象你可以得到這樣

最終結果後
String result1 = ((JSONArray) json.get("ShipTo")).getJSONObject(0) 
     .getString("ShipToAddress1"); 
String result2 = ((JSONArray) json.get("ShipTo")).getJSONObject(0) 
     .getString("ShipToAddress2"); 

試試這個,它會工作。