问题描述
我是有点有关,我收到来自服务器的响应中的JSONObject的修复程序。
I am in a bit of a fix regarding the JSONObject that I am getting as a response from the server.
jsonObj = new JSONObject(resultString);
JSONObject sync_reponse = jsonObj.getJSONObject("syncresponse");
String synckey_string = sync_reponse.getString("synckey");
JSONArray createdtrs_array = sync_reponse.getJSONArray("createdtrs");
JSONArray modtrs_array = sync_reponse.getJSONArray("modtrs");
JSONArray deletedtrs_array = sync_reponse.getJSONArray("deletedtrs");
String deleted_string = deletedtrs_array.toString();
{"syncresponse":{"synckey":"2011-09-30 14:52:00","createdtrs":[],"modtrs":[],"deletedtrs":[{"companyid":"UTB17","username":"DA","date":"2011-09-26","reportid":"31341"}]
,你可以在响应中看到,我得到我解析的JSONObject和创建syncresponse,synckey为 JSON对象 createdtrs,modtrs,deletedtrs为 JSONArray 。我想从deletedtrs访问的JSONObject,这样我就可以将它们分割开,并使用的值。也就是我想提取companyid,用户名,日期等。
as you can see in the response that I am getting I am parsing the JSONObject and creating syncresponse, synckey as a JSON object createdtrs, modtrs, deletedtrs as a JSONArray. I want to access the JSONObject from deletedtrs, so that I can split them apart and use the values. i.e I want to extract companyid, username, date etc.
我怎么能去呢?
感谢您的输入。
推荐答案
JSONArray对象有一个函数 getJSONObject(INT指数)
,你可以通过所有的JSONObjects的循环由编写一个简单的for循环:
JSONArray objects have a function getJSONObject(int index)
, you can loop through all of the JSONObjects by writing a simple for-loop:
JSONArray array;
for(int n = 0; n < array.length(); n++)
{
JSONObject object = array.getJSONObject(n);
// do some stuff....
}
这篇关于获取的JSONObject从JSONArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!