JSON_exc2

Posted Tears_fg

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JSON_exc2相关的知识,希望对你有一定的参考价值。

 1 package cn.kjxy.JSON;
 2 
 3 import java.util.List;
 4 
 5 import org.json.JSONArray;
 6 import org.json.JSONException;
 7 import org.json.JSONObject;
 8 
 9 /**
10  * 解析网络数据
11  *     path:"http://interfacev5.vivame.cn/x1-interface-v5/json/newdatalist.json?platform=android&installversion=5.2.0&channelno=OPPOA2320480100&mid=0a0fec95ea3364f43798a33fc3616780&uid=3348521&sid=a27ef935-f451-46ad-962b-0a48c47d47ae&type=-1&id=-1&category=-1&ot=0&nt=0"
12  *    步骤:1.获取路径
13  *        2.通过HttpClient从该网络路径获取json数据
14  *        3.通过bejson工具进行json校验
15  *        4.创建相关对象或数组进行json解析
16  * @author Administrator
17  *   注意: 需导入HttpClient的三个jar包及JSON的jar包
18  */
19 
20 class Item{
21      private int stypeid;//": 3,
22      private  String stypename;//: "图集",
23      private String title;//": "暴雨致赣南5.3万人受灾",
24      private String img;//": "http://stcv5.vivame.cn/pmsV5/upload/file/20160323/f116d696-c777-43a0-a709-d044dec48900.jpg",
25     public int getStypeid() {
26         return stypeid;
27     }
28     public void setStypeid(int stypeid) {
29         this.stypeid = stypeid;
30     }
31     public String getStypename() {
32         return stypename;
33     }
34     public void setStypename(String stypename) {
35         this.stypename = stypename;
36     }
37     public String getTitle() {
38         return title;
39     }
40     public void setTitle(String title) {
41         this.title = title;
42     }
43     public String getImg() {
44         return img;
45     }
46     public void setImg(String img) {
47         this.img = img;
48     }
49     @Override
50     public String toString() {
51         return "Items [stypeid=" + stypeid + ", stypename=" + stypename
52                 + ", title=" + title + ", img=" + img + "]";
53     }
54     
55 }
56 public class Demo2 {
57     public static void main(String[] args) {
58         String path = "http://interfacev5.vivame.cn/x1-interface-v5/json/newdatalist.json?platform=android&installversion=5.2.0&channelno=OPPOA2320480100&mid=0a0fec95ea3364f43798a33fc3616780&uid=3348521&sid=a27ef935-f451-46ad-962b-0a48c47d47ae&type=-1&id=-1&category=-1&ot=0&nt=0";
59         //应用自制工具类解析JSON数据
60         String json = HttpHelpers.getResourceByInternet(path);
61         parserJSON(json);
62     }
63 
64     private static void parserJSON(String json) {
65         // TODO Auto-generated method stub
66           try {
67               //创建JSON对象
68               JSONObject jsonObject = new JSONObject(json);
69               JSONObject data = jsonObject.getJSONObject("data");
70               JSONArray feedList = data.getJSONArray("feedlist");
71               for (int i = 0; i < feedList.length(); i++) {
72                 JSONObject jsonObject2 = feedList.getJSONObject(i);
73                 JSONArray items = jsonObject2.getJSONArray("items");
74                 for (int j = 0; j < items.length(); j++) {
75                     JSONObject jsonObject3 = items.getJSONObject(j);
76                     Item item =  new Item();
77                     item.setStypeid(jsonObject3.getInt("stypeid"));
78                     item.setStypename(jsonObject3.getString("stypename"));
79                     item.setTitle(jsonObject3.getString("title"));
80                     item.setImg(jsonObject3.getString("img"));
81                     System.out.println(item);
82                 }
83               
84               }
85           } catch (JSONException e) {
86             // TODO Auto-generated catch block
87             e.printStackTrace();
88         }
89           }
90 }
  1 package cn.kjxy.JSON;
  2 
  3 import java.io.BufferedInputStream;
  4 import java.io.BufferedOutputStream;
  5 import java.io.BufferedReader;
  6 import java.io.ByteArrayOutputStream;
  7 import java.io.FileNotFoundException;
  8 import java.io.FileOutputStream;
  9 import java.io.IOException;
 10 import java.util.ArrayList;
 11 import java.util.HashMap;
 12 import java.util.List;
 13 import java.util.Map;
 14 
 15 import org.apache.http.HttpEntity;
 16 import org.apache.http.HttpResponse;
 17 import org.apache.http.NameValuePair;
 18 import org.apache.http.client.HttpClient;
 19 import org.apache.http.client.entity.UrlEncodedFormEntity;
 20 import org.apache.http.client.methods.HttpGet;
 21 import org.apache.http.client.methods.HttpPost;
 22 import org.apache.http.impl.client.DefaultHttpClient;
 23 import org.apache.http.message.BasicNameValuePair;
 24 import org.apache.http.util.EntityUtils;
 25 
 26 /**
 27  * 请求网络的工具类
 28  * 
 29  * @author Administrator
 30  *
 31  */
 32 public class HttpHelpers {
 33 
 34     /**
 35      * 下载图片 保存到byte类型的数组中
 36      * 
 37      * @param path
 38      *            地址
 39      * @return byte[]
 40      */
 41     public static byte[] downLoadImg(String path) {
 42         BufferedInputStream bis = null;
 43         try {
 44             // 1,创建HttpClient对象 Android6.0之前可以使用
 45             HttpClient httpClient = new DefaultHttpClient();
 46             // 2,创建请求对象+指定地址
 47             HttpGet httpGet = new HttpGet(path);
 48             // 3,执行请求 获得HttpResponse对象
 49             HttpResponse response = httpClient.execute(httpGet);
 50             // 4,获得响应码
 51             int code = response.getStatusLine().getStatusCode();
 52             if (code == 200) {
 53                 // 5,得到响应的HttpEntity对象
 54                 HttpEntity responseEntity = response.getEntity();
 55                 // 方法一
 56                 // bis = new BufferedInputStream(responseEntity.getContent());
 57                 // byte b[] = toByteArray(bis);
 58                 // return b;
 59 
 60                 // 方法二
 61                 return EntityUtils.toByteArray(responseEntity);
 62 
 63             }
 64 
 65         } catch (IOException e) {
 66             // TODO Auto-generated catch block
 67             e.printStackTrace();
 68         } finally {
 69             if (bis != null) {
 70                 try {
 71                     bis.close();
 72                 } catch (IOException e) {
 73                     // TODO Auto-generated catch block
 74                     e.printStackTrace();
 75                 }
 76             }
 77         }
 78 
 79         return null;
 80 
 81     }
 82 
 83     /**
 84      * 把图片下载到本地磁盘
 85      * 
 86      * @param path
 87      */
 88     public static void downLoadImgToLocal(String path) {
 89         BufferedInputStream bis = null;
 90         BufferedOutputStream boStream = null;
 91         try {
 92             // 1,创建HttpClient对象 Android6.0之前可以使用
 93             HttpClient httpClient = new DefaultHttpClient();
 94             // 2,创建请求对象+指定地址
 95             HttpGet httpGet = new HttpGet(path);
 96             // 3,执行请求 获得HttpResponse对象
 97             HttpResponse response = httpClient.execute(httpGet);
 98             // 4,获得响应码
 99             int code = response.getStatusLine().getStatusCode();
100             if (code == 200) {
101                 // 5,得到响应的HttpEntity对象
102                 HttpEntity responseEntity = response.getEntity();
103                 // 方法一
104                 // bis = new BufferedInputStream(responseEntity.getContent());
105                 // byte b[] = toByteArray(bis);
106 
107                 // 方法二
108                 byte b[] = EntityUtils.toByteArray(responseEntity);
109                 String endsWith = path.substring(path.lastIndexOf("."));
110                 boStream = new BufferedOutputStream(new FileOutputStream((int) (Math.random() * 100) + endsWith));
111                 boStream.write(b);
112 
113             }
114 
115         } catch (IOException e) {
116             // TODO Auto-generated catch block
117             e.printStackTrace();
118         } finally {
119             if (bis != null) {
120                 try {
121                     bis.close();
122                 } catch (IOException e) {
123                     // TODO Auto-generated catch block
124                     e.printStackTrace();
125                 }
126             }
127             if (boStream != null) {
128                 try {
129                     boStream.close();
130                 } catch (IOException e) {
131                     // TODO Auto-generated catch block
132                     e.printStackTrace();
133                 }
134             }
135         }
136 
137     }
138 
139     /**
140      * 从互联网获取文本 json xml
141      * 
142      * @param path
143      *            地址
144      * @return 获取到的文本数据
145      */
146 
147     public static String getResourceByInternet(String path) {
148         BufferedReader bReader = null;
149         try {
150             // 1,创建HttpClient对象 Android6.0之前可以使用
151             HttpClient httpClient = new DefaultHttpClient();
152             // 2,创建请求对象+指定地址
153             HttpGet httpGet = new HttpGet(path);
154             // 3,执行请求 获得HttpResponse对象
155             HttpResponse response = httpClient.execute(httpGet);
156             // 4,获得响应码
157             int code = response.getStatusLine().getStatusCode();
158             if (code == 200) {
159                 // 得到HttpEntity对象
160                 HttpEntity responseEntity = response.getEntity();
161                 // 方法一
162                 // bReader = new BufferedReader(new
163                 // InputStreamReader(responseEntity.getContent()));
164                 // StringBuilder sbBuilder = new StringBuilder();
165                 // String line = null;
166                 // while ((line = bReader.readLine()) != null) {
167                 // sbBuilder.append(line);
168                 // }
169                 //
170                 // return sbBuilder.toString();
171 
172                 // 方法二
173                 return EntityUtils.toString(responseEntity);
174 
175             }
176 
177         } catch (IOException e) {
178             // TODO Auto-generated catch block
179             e.printStackTrace();
180         } finally {
181             if (bReader != null) {
182                 try {
183                     bReader.close();
184                 } catch (IOException e) {
185                     // TODO Auto-generated catch block
186                     e.printStackTrace();
187                 }
188             }
189         }
190 
191         return null;
192 
193     }
194 
195     public static byte[] toByteArray(BufferedInputStream bufferedInputStream) {
196         byte b[] = new byte[1024 * 1024];
197         int len = 0;
198         ByteArrayOutputStream baos = new ByteArrayOutputStream();
199         try {
200             while ((len = bufferedInputStream.read(b)) != -1) {
201                 baos.write(b, 0, len);
202             }
203         } catch (IOException e) {
204             // TODO Auto-generated catch block
205             e.printStackTrace();
206         }
207 
208         return baos.toByteArray();
209 
210     }
211 
212     public static String upLoadData(String path, Map<String, String> map) {
213         BufferedReader bReader = null;
214         try {
215             // 1,创建HttpClient对象 Android6.0之前可以使用
216             HttpClient httpClient = new DefaultHttpClient();
217             // 2,创建请求对象+指定地址
218             HttpPost httpPost = new HttpPost(path);
219             // 设置用于发送到服务端的参数
220             List<NameValuePair> list = new ArrayList<NameValuePair>();
221 
222             for (String string : map.keySet()) {
223                 list.add(new BasicNameValuePair(string, map.get(string)));
224             }
225             HttpEntity requestEntity = new UrlEncodedFormEntity(list, "gbk");
226             httpPost.setEntity(requestEntity);
227 
228             // 3,执行请求 获得HttpResponse对象
229             HttpResponse response = httpClient.execute(httpPost);
230             // 4,获得响应码
231             int code = response.getStatusLine().getStatusCode();
232             if (code == 200) {
233                 // 得到HttpEntity对象
234                 HttpEntity responseEntity = response.getEntity();
235                 // 方法一
236                 // bReader = new BufferedReader(new
237                 // InputStreamReader(responseEntity.getContent()));
238                 // StringBuilder sbBuilder = new StringBuilder();
239                 // String line = null;
240                 // while ((line = bReader.readLine()) != null) {
241                 // sbBuilder.append(line);
242                 // }
243                 //
244                 // return sbBuilder.toString();
245 
246                 // 方法二
247                 return EntityUtils.toString(responseEntity, "gbk");
248 
249             }
250 
251         } catch (IOException e) {
252             // TODO Auto-generated catch block
253             e.printStackTrace();
254         } finally {
255             if (bReader != null) {
256                 try {
257                     bReader.close();
258                 } catch (IOException e) {
259                     // TODO Auto-generated catch block
260                     e.printStackTrace();
261                 }
262             }
263         }
264 
265         return null;
266 
267     }
268 
269     public static void main(String[] args) {
270         byte b[] = downLoadImg("http://images.china.cn/attachement/jpg/site1000/20140313/844bf52c7d7c148b8abc05.jpg");
271         BufferedOutputStream bufferedOutputStream;
272         try {
273             bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("ceo.jpg"));
274             // bufferedOutputStream.write(b);
275 
276         } catch (FileNotFoundException e1) {
277             // TODO Auto-generated catch block
278             e1.printStackTrace();
279         } catch (IOException e) {
280             // TODO Auto-generated catch block
281             e.printStackTrace();
282         }
283 
284         // downLoadImgToLocal("http://images.china.cn/attachement/jpg/site1000/20140313/844bf52c7d7c148b8abc05.jpg");
285 
286         // System.out.println(getResourceByInternet("http://www.doukantv.com/api/hot/?type=movie&cli=ipad&sys_ver=7.1.1&ver=2.1"));
287         Map<String, String> map = new HashMap<String, String>();
288         map.put("uname", "张三");
289         map.put("pwd", "admin");
290         System.out.println(upLoadData("http://172.20.136.5:8080/2016_05_27_server/login", map));
291 
292     }
293 
294 }

 

以上是关于JSON_exc2的主要内容,如果未能解决你的问题,请参考以下文章

为啥代码片段在 matplotlib 2.0.2 上运行良好,但在 matplotlib 2.1.0 上引发错误

web代码片段

sublime text3 增加代码片段(snipper)

JS常用代码片段2-值得收藏

JS常用代码片段2-值得收藏

JavaScript 代码片段