解决方案 »

  1.   

    推荐Gson(一个处理json的jar包)
    如果没有,下载地址http://download.csdn.net/detail/u014772349/7899045以后为解析的代码仅供参考
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;import com.google.gson.Gson;
    import com.google.gson.internal.StringMap;public class Test { /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException { StringBuffer sbf = new StringBuffer();
    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream( "F:"+File.separator+"json.txt"),"UTF-8"));
    //json字符串
    String str="";
    while((str=reader.readLine())!=null){
    sbf.append(str);
    }
    reader.close();

    StringMap strMap=parseJson(sbf.toString());
    //把StringMap转为你自己的VO
    System.out.println(strMap);
    }

    public static StringMap parseJson(String jsonstr){
    Gson gson = new Gson();
    StringMap strMapArray=gson.fromJson(jsonstr, StringMap.class);
    return strMapArray;
    }

    }
      

  2.   

    VO:汽油ID(可选)   汽油类型  汽油型号   汽油价格  汽油优惠  恩 ,差不多就这么多了。
      

  3.   


    {"A":{"A95":{"price":666,"rebate":10},A92:{"price":888,"rebate":20}},"B":{"B0":{"price":454,"rebate":3454}}}   这组json字符串的vo对象是什么,帮忙看看,我写了个vo,但是数据获取不到数据。
      

  4.   


    {"A":{"A95":{"price":666,"rebate":10},A92:{"price":888,"rebate":20}},"B":{"B0":{"price":454,"rebate":3454}}}   这组json字符串的vo对象是什么,帮忙看看,我写了个vo,但是数据获取不到数据。
    这是写的一个vo对象
      

  5.   

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;import com.google.gson.Gson;
    import com.google.gson.internal.StringMap;public class Test { /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException { //json字符串
    String str=getJsonStr();
    //把json字符串转为自己的VO列表
    List<VO> list = convertJsonToList(str);
    //重新输出JSON(实际上组装为下面的JSON比较方面)
    Gson gson = new Gson();
    System.out.println(gson.toJson(list));
    //[{"type":"A95","pidtype":"A","price":"666.0","rebate":"10.0"},
    //{"type":"A92","pidtype":"A","price":"888.0","rebate":"20.0"},
    //{"type":"B0","pidtype":"B","price":"454.0","rebate":"3454.0"}] }


    /**
     * 获得要转换的JSON字符串<br>
     * 这边为了方面我直接读的我本地的文件内容为你要转换的json字符串
     * @return
     * @throws IOException
     */
    private static String getJsonStr() throws IOException{
    StringBuffer sbf = new StringBuffer();
    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream( "F:"+File.separator+"json.txt"),"UTF-8"));
    //json字符串
    String str="";
    while((str=reader.readLine())!=null){
    sbf.append(str);
    }
    reader.close();
    return sbf.toString();
    }

    private static StringMap parseJson(String jsonstr){
    Gson gson = new Gson();
    StringMap strMapArray=gson.fromJson(jsonstr, StringMap.class);
    return strMapArray;
    }

    /**
     * 把json字符串转为自己的VO列表
     * @param str
     * @return
     * @throws ClassNotFoundException 
     * @throws IllegalAccessException 
     * @throws InstantiationException 
     */
    private static List<VO> convertJsonToList(String str ) {
    Map<String,Map<String,Map<String,Object>>> map = parseJson(str);
    List<VO> list = new ArrayList<VO>();
    Iterator<String> iterator = map.keySet().iterator();
    while(iterator.hasNext()){
    String firstkey = iterator.next();
    Map<String,Map<String,Object>> voMap= map.get(firstkey);
    Iterator<String> iteratorVO = voMap.keySet().iterator();
    while(iteratorVO.hasNext()){
    String key = iteratorVO.next();
    Map<String,Object> map1 = voMap.get(key);
    VO vo = new VO();
    vo.setType(key);
    vo.setPidtype(firstkey);
    vo.setPrice(""+map1.get("price"));
    vo.setRebate(""+map1.get("rebate"));
    list.add(vo);
    }
    }

    return list;
    }
    }class VO {

    private String type;//当前油的种类
    private String pidtype;//当前油的所属的种类(汽油/柴油)
    private String price;//价格
    private String rebate;//优惠信息
    public String getPrice() {
    return price;
    }
    public void setPrice(String price) {
    this.price = price;
    }
    public String getRebate() {
    return rebate;
    }
    public void setRebate(String rebate) {
    this.rebate = rebate;
    }
    public String getType() {
    return type;
    }
    public void setType(String type) {
    this.type = type;
    }
    public String getPidtype() {
    return pidtype;
    }
    public void setPidtype(String pidtype) {
    this.pidtype = pidtype;
    }
    }
      

  6.   

    A:import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;import com.google.gson.Gson;
    import com.google.gson.internal.StringMap;public class Test { /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException { //json字符串
    String str=getJsonStr();
    //把json字符串转为自己的VO列表
    List<VO> list = convertJsonToList(str);
    //重新输出JSON(实际上组装为下面的JSON比较方面)
    Gson gson = new Gson();
    System.out.println(gson.toJson(list));
    //[{"type":"A95","pidtype":"A","price":"666.0","rebate":"10.0"},
    //{"type":"A92","pidtype":"A","price":"888.0","rebate":"20.0"},
    //{"type":"B0","pidtype":"B","price":"454.0","rebate":"3454.0"}] }


    /**
     * 获得要转换的JSON字符串<br>
     * 这边为了方面我直接读的我本地的文件内容为你要转换的json字符串
     * @return
     * @throws IOException
     */
    private static String getJsonStr() throws IOException{
    StringBuffer sbf = new StringBuffer();
    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream( "F:"+File.separator+"json.txt"),"UTF-8"));
    //json字符串
    String str="";
    while((str=reader.readLine())!=null){
    sbf.append(str);
    }
    reader.close();
    return sbf.toString();
    }

    private static StringMap parseJson(String jsonstr){
    Gson gson = new Gson();
    StringMap strMapArray=gson.fromJson(jsonstr, StringMap.class);
    return strMapArray;
    }

    /**
     * 把json字符串转为自己的VO列表
     * @param str
     * @return
     * @throws ClassNotFoundException 
     * @throws IllegalAccessException 
     * @throws InstantiationException 
     */
    private static List<VO> convertJsonToList(String str ) {
    Map<String,Map<String,Map<String,Object>>> map = parseJson(str);
    List<VO> list = new ArrayList<VO>();
    Iterator<String> iterator = map.keySet().iterator();
    while(iterator.hasNext()){
    String firstkey = iterator.next();
    Map<String,Map<String,Object>> voMap= map.get(firstkey);
    Iterator<String> iteratorVO = voMap.keySet().iterator();
    while(iteratorVO.hasNext()){
    String key = iteratorVO.next();
    Map<String,Object> map1 = voMap.get(key);
    VO vo = new VO();
    vo.setType(key);
    vo.setPidtype(firstkey);
    vo.setPrice(""+map1.get("price"));
    vo.setRebate(""+map1.get("rebate"));
    list.add(vo);
    }
    }

    return list;
    }
    }class VO {

    private String type;//当前油的种类
    private String pidtype;//当前油的所属的种类(汽油/柴油)
    private String price;//价格
    private String rebate;//优惠信息
    public String getPrice() {
    return price;
    }
    public void setPrice(String price) {
    this.price = price;
    }
    public String getRebate() {
    return rebate;
    }
    public void setRebate(String rebate) {
    this.rebate = rebate;
    }
    public String getType() {
    return type;
    }
    public void setType(String type) {
    this.type = type;
    }
    public String getPidtype() {
    return pidtype;
    }
    public void setPidtype(String pidtype) {
    this.pidtype = pidtype;
    }
    }

    漂亮!
      

  7.   

    实体和json 的字符串名称对应好,然后映射。