android 对象 引用,从Android中检索JSON中的引用对象

{

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中现在正在填充。

2013-03-13

cmnunis

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值