第1步:复制WEBSERVICE URL并粘贴到您的浏览器中,这将访问Web服务并向您显示响应,使用chrome可以更方便地查看JSON响应
第2步:分析JSON响应的结构
首先,您将以字符串形式阅读完整的回复
从字符串创建一个JSON对象
现在将JSON对象转换为JSONARRAY对象,
现在您有了一个JSONARRAY
迭代JSON数组并一一存储对象
在JSON数组的迭代循环中,对于每个JSON OBJECT调用其值
名称
在JSON中看到您有键值对
您可以调用JSONOBJECT.getString(“检索字符串的变量名称”);
或者您也可以获取其他类似的数据类型
自行尝试,向我发送状态,将通过修改后的代码回复您
然后
?============================================ =================
我试图为您解决问题,这是课程
package com.hussain.StackOverFlow;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class FarhaSameer1 {
public static void main(String[] args)
{
String asd = FarhaSameer1.sendRequest("http://gdata.youtube.com/feeds/base/videos?max-results=10&start-//index=1&alt=json&orderby=published&author=astrobixweb");
FarhaSameer1.parseFromJSONResponse(asd);
}
// API link
// http://gdata.youtube.com/feeds/base/videos?max-results=10&start-//index=1&alt=json&orderby=published&author=astrobixweb
// String Method to fetech data from server
public static String sendRequest(String url) {
String result = "";
try {
HttpClient client = new DefaultHttpClient();
HttpParams httpParameters = client.getParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
HttpConnectionParams.setSoTimeout(httpParameters, 5000);
HttpConnectionParams.setTcpNoDelay(httpParameters, true);
HttpGet request = new HttpGet();
request.setURI(new URI(url));
HttpResponse response = client.execute(request);
InputStream ips = response.getEntity().getContent();
BufferedReader buf = new BufferedReader(new InputStreamReader(ips,"UTF-8"));
StringBuilder sb = new StringBuilder();
String s;
while (true) {
s = buf.readLine();
if (s == null || s.length() == 0)
break;
sb.append(s);
}
buf.close();
ips.close();
result = sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void parseFromJSONResponse(String respo)
{
JSONObject myjson;
try
{
myjson = new JSONObject(respo);
JSONObject jsonObj1 = myjson.getJSONObject("feed");
JSONArray jsonObj2 = jsonObj1.getJSONArray("entry");
JSONObject jsonObj3 = jsonObj2.getJSONObject(0);
System.out.println(jsonObj3.getJSONObject("content"));
System.out.println("here ===>>>"+jsonObj3.getJSONObject("content").get("$t").toString());
}
catch (JSONException e) {
e.printStackTrace();
}
}
}
看到第一个方法和你写的一样
在第二种方法中,我试图逐步遍历JSON响应.
看到您必须谨慎对待JSON响应
1:您的完整响应是JSON对象
2:如果有任何类似的内容
"some key name " : { " some value " }
这是一个JSON对象
3:如果像这样写任何元素
"some key name " : " some value "
这是您可以通过json对象获取的值
jsonObject.getString("key name")
4:如果写入任何元素,例如
"some key name " : [ " some value " ]
那么这是一个JSON数组,您必须将其放入JSON ARRAY,然后遍历其元素
jsonObject.getJSONARRAY("key name for JSON ARRAY IN RESPONSE ")
然后您可以遍历JSON ARRAY的元素
`jsonArrayObj.get(0);`
现在您可以遍历并检索所需的值,如果需要任何帮助,请发邮件给我