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,到目前为止,我没有遇到任何问题。但是,我有一个小问题,这与检索寄售类引用的对象有关,即 ShipTo 和 Client 类。

到目前为止,我能够检索寄售值,例如 ConsignmentActive、ConsignmentCreationDate、ConsignmentCustRef、ConsignmentID 和 OrderNo。但是,我不确定如何映射 ShipTo 和 Client 中的项目。

这是我当前的代码,我试图在一个方法中执行:-

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 中的我的 Consignment、ShipTo 和 Clients 对象。

4

1 回答 1

0

首先像这样解析响应

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

得到响应后,像这样从响应中创建一个json 对象

JSONObject json = new JSONObject(response);

你可以得到这样的最终结果

    String result1 = ((JSONArray) json.get("ShipTo")).getJSONObject(0)
        .getString("ShipToAddress1");
String result2 = ((JSONArray) json.get("ShipTo")).getJSONObject(0)
        .getString("ShipToAddress2");

试试这个,它会工作的。

于 2013-03-13T07:58:33.303 回答