java通过url请求服务器端json数据并解析成想要的数据
2015-01-20 16:27
429 查看
最近做项目做到关于java json解析的部分记录一下 方便以后用到。。。。
废话少说贴代码。
String url="http://。。。。。。。。。";
StringBuffer json = new StringBuffer();
String strjson;
StringBuffer indexStrBuf = new StringBuffer();
try {
这都通过是读文件的固定格式。。。。
URL urlobj=new URL(url);
URLConnection uc = urlobj.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
String inputLine=null;
while ((inputLine = in.readLine())!=null){
json.append(inputLine);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
strjson=json.toString();
//呵呵。。。因为返回的数据是jsonp格式 为了去掉 恶心的头部信息,我使用的是强大的正则表达式
Pattern pattern = Pattern.compile("\\bdoc\".*");//正则表达式
Matcher matcher = pattern.matcher(strjson); //你需要对它使用正则表达式的字符串
while (matcher.find()) {
strjson=matcher.group(); //其实应该存储到一个数组,或者使用stringbuffer里面但是,因为我的数据只有一条 所以我就懒得管了
}
System.out.println(strjson);
strjson=strjson.substring(5, strjson.length()-3);//呵呵还是为了处理不规整的json字符串
System.out.println(strjson);
//下面开始解析json字符串
JSONArray jsonarr=null;
JSONObject jsonobj=null;
try {
jsonarr=new JSONArray(strjson);
int i;
int len=jsonarr.length();
if(len>5)len=5;
for (i = 0; i < len ;i++) {
jsonobj= jsonarr.getJSONObject(i);
//下面这些都是根据需求去转的
String title=jsonobj.getJSONObject("item").getJSONObject("display").getString("title");
String pic=jsonobj.getJSONObject("item").getJSONObject("display").getJSONObject("thumbnails").getString("thumbnail");
String link=jsonobj.getJSONObject("item").getJSONObject("display").getString("url");
String price=jsonobj.getJSONObject("item").getJSONObject("display").getJSONObject("prices").getJSONObject("price").getString("value");
String discount=jsonobj.getJSONObject("item").getJSONObject("display").getString("discount");
String orlprice;
if(!"0".equals(discount)){
double prc=Double.valueOf(price);
double dis=Double.valueOf(discount);
double orlprc=prc/dis;
orlprice=((int)Math.rint(orlprc))+"";
}else{
orlprice=price;
}
discount=String.format("%.1f", Double.valueOf(discount)*10);
String baoyou;
if(jsonobj.toString().indexOf("free_ship")!=-1){
baoyou=jsonobj.getJSONObject("item").getJSONObject("display").getString("free_ship");
}else{
baoyou="0";
}
String list = "[\"" + title + "\",\"" + pic + "\",\"" + link + "\",\"" + price + "\",\"" + orlprice + "\",\"" + discount + "\",\"" + baoyou + "\"],";
indexStrBuf.append(list);
}
} catch (JSONException e) {
e.printStackTrace();
}
String liststr=indexStrBuf.toString();
int length=liststr.length();
liststr=liststr.substring(0, length-1);
最后就是转换成我想要的json了 ,搞定
String jsonstr="{desc:{title:0,pic:1,link:2,price:3,orlprice:4,discount:5,baoyou:6},list:[" + liststr+ "]}";
System.out.println(jsonstr);
废话少说贴代码。
String url="http://。。。。。。。。。";
StringBuffer json = new StringBuffer();
String strjson;
StringBuffer indexStrBuf = new StringBuffer();
try {
这都通过是读文件的固定格式。。。。
URL urlobj=new URL(url);
URLConnection uc = urlobj.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
String inputLine=null;
while ((inputLine = in.readLine())!=null){
json.append(inputLine);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
strjson=json.toString();
//呵呵。。。因为返回的数据是jsonp格式 为了去掉 恶心的头部信息,我使用的是强大的正则表达式
Pattern pattern = Pattern.compile("\\bdoc\".*");//正则表达式
Matcher matcher = pattern.matcher(strjson); //你需要对它使用正则表达式的字符串
while (matcher.find()) {
strjson=matcher.group(); //其实应该存储到一个数组,或者使用stringbuffer里面但是,因为我的数据只有一条 所以我就懒得管了
}
System.out.println(strjson);
strjson=strjson.substring(5, strjson.length()-3);//呵呵还是为了处理不规整的json字符串
System.out.println(strjson);
//下面开始解析json字符串
JSONArray jsonarr=null;
JSONObject jsonobj=null;
try {
jsonarr=new JSONArray(strjson);
int i;
int len=jsonarr.length();
if(len>5)len=5;
for (i = 0; i < len ;i++) {
jsonobj= jsonarr.getJSONObject(i);
//下面这些都是根据需求去转的
String title=jsonobj.getJSONObject("item").getJSONObject("display").getString("title");
String pic=jsonobj.getJSONObject("item").getJSONObject("display").getJSONObject("thumbnails").getString("thumbnail");
String link=jsonobj.getJSONObject("item").getJSONObject("display").getString("url");
String price=jsonobj.getJSONObject("item").getJSONObject("display").getJSONObject("prices").getJSONObject("price").getString("value");
String discount=jsonobj.getJSONObject("item").getJSONObject("display").getString("discount");
String orlprice;
if(!"0".equals(discount)){
double prc=Double.valueOf(price);
double dis=Double.valueOf(discount);
double orlprc=prc/dis;
orlprice=((int)Math.rint(orlprc))+"";
}else{
orlprice=price;
}
discount=String.format("%.1f", Double.valueOf(discount)*10);
String baoyou;
if(jsonobj.toString().indexOf("free_ship")!=-1){
baoyou=jsonobj.getJSONObject("item").getJSONObject("display").getString("free_ship");
}else{
baoyou="0";
}
String list = "[\"" + title + "\",\"" + pic + "\",\"" + link + "\",\"" + price + "\",\"" + orlprice + "\",\"" + discount + "\",\"" + baoyou + "\"],";
indexStrBuf.append(list);
}
} catch (JSONException e) {
e.printStackTrace();
}
String liststr=indexStrBuf.toString();
int length=liststr.length();
liststr=liststr.substring(0, length-1);
最后就是转换成我想要的json了 ,搞定
String jsonstr="{desc:{title:0,pic:1,link:2,price:3,orlprice:4,discount:5,baoyou:6},list:[" + liststr+ "]}";
System.out.println(jsonstr);
相关文章推荐
- 通过JAVA从高德地图URL连接获取json数据 解析并存入数据库的程序举例
- 怎么获取pm2.5数据----pm2.5 的获取 java 通过url获取后,得到json 格式,在解析json
- Java根据URL连接请求JSON数据解析代码
- Java根据URL连接请求JSON数据解析代码
- 关于java 发送http json数据格式请求时,服务器端如何接收json数据并解析
- java通过URL读取json数据
- dom4j来解析xml,通过URL获取服务器端返回的字符串,java swing 实现的页面
- java通过URL读取json数据
- Android初识-通过URL地址请求并返回JSON数据
- java通过URL读取json数据
- java通过URL读取json数据
- 微信相关 -- 可以通过调用下面的方法 请求对应的url 获取微信服务器返回的json数据
- HttpURLConnection请求数据(GET)并用JSON解析数据(二)
- android菜鸟学习笔记25----与服务器端交互(二)解析服务端返回的json数据及使用一个开源组件请求服务端数据
- android:通过url向网络端获取json格式数据并解析显示
- android通过httpClient请求获取JSON数据并且解析
- c# 通过访问URL,获取并解析远程服务端返回的复杂Json数据
- android通过httpClient请求获取JSON数据并且解析
- 通过java代码对json格式数据进行解析
- android通过httpClient请求获取JSON数据并且解析