读取网址中json数据 转换为java格式

http://yu-duo.iteye.com/blog/1684846

 

内容描述: 
条件: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一篇博客,使用了其代码,虽然方法很老,但是解决问题了,要把这个问题涉及的知识点好好看一下梳理一下。 


全部实现代码如下: 

Java代码  收藏代码
    1. import java.io.BufferedReader;  
    2. import java.io.InputStream;  
    3. import java.io.InputStreamReader;  
    4. import java.util.List;  
    5.   
    6. import org.apache.http.HttpEntity;  
    7. import org.apache.http.HttpResponse;  
    8. import org.apache.http.client.HttpClient;  
    9. import org.apache.http.client.methods.HttpGet;  
    10. import org.apache.http.client.params.HttpClientParams;  
    11. import org.apache.http.impl.client.DefaultHttpClient;  
    12. import org.apache.http.params.BasicHttpParams;  
    13. import org.apache.http.params.HttpConnectionParams;  
    14. import org.apache.http.params.HttpParams;  
    15. import org.codehaus.jackson.map.ObjectMapper;  
    16. import org.codehaus.jackson.type.TypeReference;  
    17.   
    18. public class ReadingWebContent {    
    19.     /** 
    20.      *  
    21.      * @param url 
    22.      * @return 
    23.      * @throws Exception 
    24.      */  
    25.     public static String getContent(String url) throws Exception{   
    26.         String CONTENT_CHARSET = "UTF-8";  
    27.         String backContent = null;    
    28. //        private static final END_OBJECT = "0";  
    29.         String eND_OBJECT = "0";  
    30.          
    31.         HttpClient httpclient = null;    
    32.         HttpGet httpget = null;     
    33.         try {    
    34.               
    35.             HttpParams params = new BasicHttpParams();    
    36.                
    37.             HttpConnectionParams.setConnectionTimeout(params, 180 * 1000);    
    38.             HttpConnectionParams.setSoTimeout(params, 180 * 1000);    
    39.             HttpConnectionParams.setSocketBufferSize(params, 8192);    
    40.               
    41.             HttpClientParams.setRedirecting(params, false);    
    42.                   
    43.             httpclient = new DefaultHttpClient(params);    
    44.              
    45.             httpget = new HttpGet(url);                 httpclient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, CONTENT_CHARSET);    
    46.             HttpResponse response = httpclient.execute(httpget);     
    47.               
    48.             HttpEntity entity = response.getEntity();    
    49.             if (entity != null) {                
    50.                   
    51.                 InputStream is = entity.getContent();    
    52.                 BufferedReader in = new BufferedReader(new InputStreamReader(is));     
    53.                 StringBuffer buffer = new StringBuffer();     
    54.                 String line = "";    
    55.                 while ((line = in.readLine()) != null) {    
    56.                     buffer.append(line);    
    57.                 }    
    58.                  
    59.                 backContent = buffer.toString();    
    60.             }    
    61.         } catch (Exception e) {    
    62.             httpget.abort();    
    63.             backContent = "���쳣,��ȡ����";       
    64.             System.out.println("-------------�쳣��ʼ");    
    65.             e.printStackTrace();    
    66.             System.out.println("-------------�쳣����");    
    67.         }finally{    
    68.               
    69.             if(httpclient != null)    
    70.                 httpclient.getConnectionManager().shutdown();    
    71.         }             
    72.         return backContent;    
    73.     }    
    74.   
    75.     @SuppressWarnings("static-access")    
    76.     public static void main(String[] args) throws Exception{    
    77.         ReadingWebContent a = new ReadingWebContent();    
    78.         String address = a.getContent("http://www.aaa/Bbb/ccclist.json  
    79.          
    80.         String data = address.substring(address.indexOf("result") + 8, address.length() -1 );  
    81.         ObjectMapper mapper = new ObjectMapper();  
    82.         List<Company> list = mapper.readValue(data, new TypeReference<List<Company>>(){});  
    83.   
    84.         for (int i = 0; i < list.size(); i++) {  
    85.               
    86.             System.out.println(list.get(i).getCompany_id());  
    87.             System.out.println(list.get(i).getCompany_name());  
    88.         }  
    89.         System.out.println(list.size());  
    90.   
    91.     }    
    92. }    
posted on 2012-09-25 17:25  adolfmc  阅读(5573)  评论(0编辑  收藏  举报