首页 > 编程 > Java > 正文

java 解析 json

2019-11-08 01:15:58
字体:
来源:转载
供稿:网友
package com.cn.dzg.util;import java.io.ByteArrayOutputStream;    import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.Map;import com.alibaba.fastjson.JSONException;import com.cn.dzg.pojo.Person;import net.sf.json.JSONArray;import net.sf.json.JSONObject;public class CheckGaoDe {public static String getJsonContent(String urlStr){        try{// 获取HttpURLConnection连接对象            URL url = new URL(urlStr);            HttpURLConnection httpConn = (HttpURLConnection) url                    .openConnection();            // 设置连接属性            httpConn.setConnectTimeout(3000);            httpConn.setDoInput(true);            httpConn.setRequestMethod("GET");            // 获取相应码            int respCode = httpConn.getResponseCode();            if (respCode == 200){                return ConvertStream2Json(httpConn.getInputStream());            }        }        catch (MalformedURLException e)        {            e.PRintStackTrace();        }        catch (IOException e)        {            e.printStackTrace();        }        return "";    } public static String ConvertStream2Json(InputStream inputStream){        String jsonStr = "";        // ByteArrayOutputStream相当于内存输出流        ByteArrayOutputStream out = new ByteArrayOutputStream();        byte[] buffer = new byte[1024];        int len = 0;        // 将输入流转移到内存输出流中        try        {            while ((len = inputStream.read(buffer, 0, buffer.length)) != -1)            {                out.write(buffer, 0, len);            }            // 将内存流转换为字符串            jsonStr = new String(out.toByteArray());        }        catch (IOException e)        {            // TODO Auto-generated catch block            e.printStackTrace();        }        return jsonStr;    }//2、获取javaBean public static Person getPerson(String jsonStr){Person person = new Person();try{// 将json字符串转换为json对象    JSONObject jsonObj = new JSONObject();    jsonObj.put("jsonStr", jsonStr);    // 得到指定json key对象的value对象    JSONObject personObj = jsonObj.getJSONObject("results");    // 获取之对象的所有属性    person.setSname(personObj.getString("address"));}catch (net.sf.json.JSONException e){    // TODO Auto-generated catch block    e.printStackTrace();}return person;}   public static List<Person> getPersons(String jsonStr)    {        List<Person> list = new ArrayList<Person>();        JSONObject jsonObj;        try        {// 将json字符串转换为json对象            jsonObj = new JSONObject();            jsonObj.put("jsonStr", jsonStr);                        // 得到指定json key对象的value对象            JSONArray personList = jsonObj.getJSONArray("persons");            Iterator<Person> iterator = personList.iterator();            List<Person> persons = new ArrayList<Person>();            if (iterator.hasNext()) {            Person p = iterator.next();            persons.add(p);}             // 遍历jsonArray            for (int i = 0; i < persons.size(); i++){                // 获取每一个json对象                JSONObject jsonItem = personList.getJSONObject(i);                // 获取每一个json对象的值                Person person = new Person();                person.setSname(jsonItem.getString("address"));                list.add(person);            }        }        catch (JSONException e)        {            e.printStackTrace();        }        return list;    }    public static String comp(String shopName){JSONObject jsonObject = JSONObject.fromObject(getJsonContent("http://restapi.amap.com/v3/place/text?&keyWords="+shopName+"&city=%E5%8C%97%E4%BA%AC&output=json&key=cc67375e756f812a64a3ffcb2719ea4e"));JSONArray jsonArray = jsonObject.getJSONArray("pois"); String address = "";if (jsonArray.size()==1) {address = jsonArray.getJSONObject(0).get("address").toString();}else{for (int i = 0; i < jsonArray.size(); i++) {    if (shopName.equals(jsonArray.getJSONObject(i).get("name"))) {  address = jsonArray.getJSONObject(i).get("address").toString();}else{address=jsonArray.getJSONObject(i).get("address").toString();}}}  return address; }} 
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表