![](/img/trans.png)
[英]How to parse Multiple JSON Objects and arrays from a url in android? i am using a example i want it in that example,
[英]How to Parse only some selected Json arrays and Json Objects from a Url to android device
我有一个Json数据,它包含多个JSON数组和JSON对象,所以我想在android活动中解析其中的一些,...
这里有JSON数组和Json对象的示例
所以我想在Android设备中调用其中的一些....我知道如何解析,但我只想要一些内容,..
我有像这样的数据
{
"process":"done"
"one":1
"List": {
"Something": [
{
"Name": "John",
"phone": "test"
}
]
"details":"ok"
"two":2
"SomethingElse": [
{
"Name": "Smith",
"phone": "test"
}
]
}
}
就像我有一个Restful服务,它有很多数据,..所以,如果想要调用“列表”或“SomethingElse”jsonObjecs /数组....它没有调用它在开始时,...
您可以考虑以SAX方式解析数据,只需遍历您的节点,如果您需要解析它,否则跳过它然后继续。 看看这里 ,JSONReader是要走的路。
上面的响应的Json数组将是这样的
JSONObject JObject = new JSONObject(response);
String Process = JObject.getstring("process");
String one= JObject.getstring("one");
JSONObject Listobject= JObject.getjsonobject("List");
JsonArray something =Listobject.getjsonarray("Something");
for(int i = 0 ; i < something.length(); i++){
JsonObject somethingobject =something.getjsonobject(i);
String name=somethingobject.getstring("Name");
String phone=somethingobject.getstring("phone");
}
String details= JObject.getstring("details");
String two= JObject.getstring("two");
JsonArray SomethingElse=JObject.getjsonarray("SomethingElse");
for(int j = 0 ; j < SomethingElse.length(); j++){
JsonObject SomethingElseobject =SomethingElse.getjsonobject(j);
String name1=SomethingElseobject .getstring("Name");
String phone1=SomethingElseobject .getstring("phone");
}
从服务器获取数据的代码
public void run() {
Log.i("run method", "calling run method");
try {
if (method == HttpMethodType.GET) {
response(executeHttpGet());
} else {
response(executeHttpPost());
}
} catch (ConnectTimeoutException ex) {
exception("Please retry after sometime...");
} catch (UnknownHostException e) {
exception("Server might be down...");
} catch (IOException e) {
exception("Please check your internet connectivity...");
} catch (Exception e) {
exception(e.getMessage());
}
Log.i(tag, "Http Call Finish");
}
private void response(String response) {
if (resListener != null) {
resListener.handleResponse(response);
}
}
private void exception(String exception) {
if (excepListener != null) {
excepListener.handleException(exception);
}
}
public String executeHttpGet() throws Exception {
Log.i("calling method", "calling execute");
Log.i("path in method", path);
BufferedReader in = null;
String page = null;
try {
HttpGet request = new HttpGet(path);
HttpResponse response = client.execute(request,localContext);
Log.i("======", response.toString());
in = new BufferedReader(new InputStreamReader(response.getEntity()
.getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
while ((line = in.readLine()) != null) {
Log.i("the response is ::", line);
sb.append(line);
}
in.close();
page = sb.toString();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return page;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.