java中的json解析

阅读更多
package com.teamdev.jxbrowser.chromium.demoTest.o2o;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class readText {
public  static String readTxt(String filePath) {
 
  try {
    File file = new File(filePath);
    if(file.isFile() && file.exists()) {
      InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "gbk");
      BufferedReader br = new BufferedReader(isr);
      String lineTxt = null;
      String text="";
      StringBuffer sb = new StringBuffer();
      while ((lineTxt = br.readLine()) != null) {
      text = sb.append(lineTxt).toString();
      }
      isr.close();
      br.close();
      return text;
    } else {
      System.out.println("文件不存在!");
    }
  } catch (Exception e) {
    System.out.println("文件读取错误!");
  }
  return null;
}
 
  public static void main(String[] args) throws JSONException {
    String filePath = "E:\\android\\content.txt";
    String content= readTxt(filePath);
    //System.out.println(content);
    //String sourceStr = "1,2,3,4,5";
    String splitName="分割线";
    //System.out.println(splitName);
        String[] sourceStrArray = content.split(splitName);
        //System.out.println(sourceStrArray[0]);
        for (int i = 0; i < sourceStrArray.length; i++) {
        if(i<=4){
        String  IndexContent=sourceStrArray[i];
        System.out.println(IndexContent);
        JSONObject ojson = new JSONObject(IndexContent);
        JSONObject oojson=(JSONObject) ojson.get("data");
        //System.out.println(oojson);
        JSONArray ajson;
try {
ajson = (JSONArray) oojson.get("poilist");
} catch (Exception e) {
// TODO Auto-generated catch block
JSONObject oojson2=(JSONObject) oojson.get("optimization_channel");
ajson = (JSONArray) oojson2.get("poi_list");
}
       //循环外创建对象,节省消耗
        JSONObject indexJson=null;
        for(int j = 0 ; j < ajson.length() ; j++){
        indexJson = (JSONObject) ajson.get(j);
        String name;
try {
name = (String) indexJson.get("name");
} catch (Exception e) {
// TODO Auto-generated catch block
name = (String) indexJson.get("title");
}
        System.out.println(name);
        }
        }
        }
       
  }

}





{"data":{"optimization_channel":{"block_title":"为你优选","block_icon":"","title_color":"#333333","poi_list":[{"wm_poi_id":278362,"title":"肯德基宅急送(苏州桥店)","pic":"http://p0.meituan.net/xianfu/0016f9e2ec997b0ff9f744ec0b9142d415083.jpeg.webp","click_url":"meituanwaimai://waimai.meituan.com/menu?restaurant_id=278362&spu_id=0","label":"口味相仿喜欢"},{"wm_poi_id":715616,"title":"必胜客(中关村广场店)","pic":"http://p0.meituan.net/xianfu/3f1c9312521f11c8a920f3a022b2a9824664.jpeg.webp","click_url":"meituanwaimai://waimai.meituan.com/menu?restaurant_id=715616&spu_id=0","label":"明星光顾的店"},{"wm_poi_id":650103,"title":"将太无二(北京欧美汇餐厅)","pic":"http://p1.meituan.net/xianfu/7fde850e890b9497217e483f7180f4e310263.jpeg.webp","click_url":"meituanwaimai://waimai.meituan.com/menu?restaurant_id=650103&spu_id=0","label":"点评高分店铺"},{"wm_poi_id":1981681,"title":"南西老北京炸鸡","pic":"http://p1.meituan.net/xianfu/48e57683e2ee1560b3ded5888967990e18465.jpg.webp","click_url":"meituanwaimai://waimai.meituan.com/menu?restaurant_id=1981681&spu_id=0","label":"口味相仿喜欢"},{"wm_poi_id":495,"title":"宏状元(苏州街店)","pic":"http://p0.meituan.net/xianfu/c213865560e79f39da5db77e816f23d210498.jpeg.webp","click_url":"meituanwaimai://waimai.meituan.com/menu?restaurant_id=495&spu_id=0","label":"点评高分店铺"},{"wm_poi_id":379288,"title":"面香八方(苏州街店)","pic":"http://p0.meituan.net/xianfu/99323e758e33f21afbf7a80ec2a1b57f28236.jpeg.webp","click_url":"meituanwaimai://waimai.meituan.com/menu?restaurant_id=379288&spu_id=0","label":"点评高分店铺"}],"more_entrance":{"show_switch":true,"text":"更多","click_url":"http://i.meituan.com/firework/weiniyouxuan52?activity_id=9398"}},"specialty_channel":{"block_title":"品质专区","block_icon":"","title_color":"#333333","list":[{"id":6,"title":"品牌商家","pic":"http://p1.meituan.net/jungle/f500d589a5a849bdb196d980291224f316405.png.webp","click_url":"http://i.waimai.meituan.com/brandchannel/home"},{"id":7,"title":"精品套餐","pic":"http://p0.meituan.net/jungle/6c935b81bc545e67c695115ae720cf5722420.png.webp","click_url":"http://i.waimai.meituan.com/foodcombo/index"},{"id":9,"title":"点评高分","pic":"http://p0.meituan.net/jungle/4eafb42ed3eb258a22b880b26dddde345253.png.webp","click_url":"http://i.meituan.com/firework/161121dianpingtuijian?activity_id=10529"}]},"ad_banner_list":[]},"code":0,"msg":"成功"}


你可能感兴趣的:(java,json)