json怎么解析啊?怎么得到具体数据啊?
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletEx...
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String tel=request.getParameter("tel");
URL url=new URL("http://apistore.baidu.com/microservice/mobilephone?tel="+tel);
HttpURLConnection connection=(HttpURLConnection) url.openConnection();//打开链接
BufferedReader br=new BufferedReader
(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder sb=new StringBuilder();
while((line=br.readLine())!=null){
sb.append(line);
}
System.out.println(sb.toString());
}
sb里面数据是{"errNum":0,"errMsg":"success","retData":{"telString":"15250471390","province":"\u6c5f\u82cf","carrier":"\u6c5f\u82cf\u79fb\u52a8"}}
我想得到 telString ,province , carrier ,具体怎么写代码啊?? 展开
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String tel=request.getParameter("tel");
URL url=new URL("http://apistore.baidu.com/microservice/mobilephone?tel="+tel);
HttpURLConnection connection=(HttpURLConnection) url.openConnection();//打开链接
BufferedReader br=new BufferedReader
(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder sb=new StringBuilder();
while((line=br.readLine())!=null){
sb.append(line);
}
System.out.println(sb.toString());
}
sb里面数据是{"errNum":0,"errMsg":"success","retData":{"telString":"15250471390","province":"\u6c5f\u82cf","carrier":"\u6c5f\u82cf\u79fb\u52a8"}}
我想得到 telString ,province , carrier ,具体怎么写代码啊?? 展开
1个回答
展开全部
1、在网上找一个json处理包,json-lib.jar
2、JSONObject json = new JSONObject();
json = JSONObject.fromObject(sb);
String telString = json.getJSONObject("retData") .getString("telString ");
String province = json.getJSONObject("retData") .getString("province ");
String carrier = json.getJSONObject("retData") .getString("carrier ");
http://www.cnblogs.com/xwdreamer/archive/2011/12/16/2296904.html 别人写的
2、JSONObject json = new JSONObject();
json = JSONObject.fromObject(sb);
String telString = json.getJSONObject("retData") .getString("telString ");
String province = json.getJSONObject("retData") .getString("province ");
String carrier = json.getJSONObject("retData") .getString("carrier ");
http://www.cnblogs.com/xwdreamer/archive/2011/12/16/2296904.html 别人写的
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询