内容描述:
条件:http://www.aaa/Bbb/ccclist.json
将条件中的json数据转换为javalist形式;
思路(脉络):
1. 读取json字符串;
2. 分析字符串格式;
3. 取出我需要的字符串(按照我需要的格式取出字符串);
4. 查看字符串编码格式是否正确
5. 使用json类库转换为beanlist形式;
6. bean中属性格式要和字符串中的对应;
继续想:
1.读取网址中的json数据需要使用httpclient/httpUrLconnettion读取网址内容;
2.字符串格式如下:{"stutus":2,"result":[{"company_name":"a公司","company_id":"-1"}, {"company_name":"222","company_id":"-55"},{"company_name":"撒旦法接 口"company_id":"-33"},{"company_name":"多的"company_id":"-34"}]}
分析格式:此字符串可以看做map的嵌套,
Map
Map<”status”,”2”>
Map <”result”,”list”>
Map<”company_name”,company.get(company_id)>
Map<”company_id”,company.get(company_name)>
3.取出需要的数据
我需要的数据位result中的list,所以用截取字符串方法将其取出来,这里用到了从0学java总截取字符串数据的的内容substring、indexof这两个知识点。
4.查看字符串格式是否正确
常用编码格式为utf-8,坚持文件属性的编码格式,将其更改为utf-8,或者在读取时用getmethod设置utf-8。
5.使用json类库转换为beanlist形式
首先声明一个objectmapper对象,使用其中的TypeReference方法将我们处理好的数据自动解析为bean形式,存放用该bean类型的list中。
最后遍历输出list中存放内容即可。
声明:此处读取数据来源为iteye一篇博客,使用了其代码,虽然方法很老,但是解决问题了,要把这个问题涉及的知识点好好看一下梳理一下。
全部实现代码如下:
import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.params.HttpClientParams; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.type.TypeReference; public class ReadingWebContent { /** * * @param url * @return * @throws Exception */ public static String getContent(String url) throws Exception{ String CONTENT_CHARSET = "UTF-8"; String backContent = null; // private static final END_OBJECT = "0"; String eND_OBJECT = "0"; HttpClient httpclient = null; HttpGet httpget = null; try { HttpParams params = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(params, 180 * 1000); HttpConnectionParams.setSoTimeout(params, 180 * 1000); HttpConnectionParams.setSocketBufferSize(params, 8192); HttpClientParams.setRedirecting(params, false); httpclient = new DefaultHttpClient(params); httpget = new HttpGet(url); httpclient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, CONTENT_CHARSET); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream is = entity.getContent(); BufferedReader in = new BufferedReader(new InputStreamReader(is)); StringBuffer buffer = new StringBuffer(); String line = ""; while ((line = in.readLine()) != null) { buffer.append(line); } backContent = buffer.toString(); } } catch (Exception e) { httpget.abort(); backContent = "????,???????"; System.out.println("-------------?????"); e.printStackTrace(); System.out.println("-------------??????"); }finally{ if(httpclient != null) httpclient.getConnectionManager().shutdown(); } return backContent; } @SuppressWarnings("static-access") public static void main(String[] args) throws Exception{ ReadingWebContent a = new ReadingWebContent(); String address = a.getContent("http://www.aaa/Bbb/ccclist.json String data = address.substring(address.indexOf("result") + 8, address.length() -1 ); ObjectMapper mapper = new ObjectMapper(); List<Company> list = mapper.readValue(data, new TypeReference<List<Company>>(){}); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i).getCompany_id()); System.out.println(list.get(i).getCompany_name()); } System.out.println(list.size()); } }
1 楼
jinnianshilongnian
2012-09-25
呵呵 终于搞定了啊
2 楼
yu_duo
2012-09-25
jinnianshilongnian 写道
呵呵 终于搞定了啊
恩~~非常感谢你的指导~~~