爱玺玺

爱玺玺的生活日记本。wx:lb87626

java解析键名是数字的json,需要将json转成map集合

 

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import utils.Utils;

public class Test5 {

 public static void main(String[] args) {
  try {
   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
   Date date = new Date(System.currentTimeMillis());
   String format = formatter.format(date);

   String postUrl = "https://e.dianping.com/shopdiy/report/common/ajax/queryTableDataList";
   String postParm = "clientKey=cpc.shop.promotion.list&tabIds=T30001%2CT30002%2CT30003&beginDate="
     + format
     + "&endDate="
     + format
     + "&launchIds=28164134%2C27906310%2C27849842%2C27808750%2C27747513%2C27747501%2C27747359%2C27747344%2C27747331%2C27747239%2C27031473%2C26653106%2C26653105%2C26641623%2C26641622%2C26641617%2C26641612%2C26641610%2C26641608%2C26212932";
   String readQueryTableDataList = Utils.readContentFromPost(postUrl,
     postParm);

   JSONObject jsonObject = JSON.parseObject(readQueryTableDataList);
   Map<String, Object> map = jsonObject;
   
   Object mapValue = Utils.getMapValue(map, "msg");

   System.out.println(mapValue);

  } catch (Exception e) {
   e.printStackTrace();
  }

 }


}



package utils;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

public class Utils {
 public static String cookie="_lxsdk_cuid=1768805c30fc8-0a82e97e1698ca-454c092b-1fa400-1768805c310c8; _lxsdk=1768805c30fc8-0a82e97e1698ca-454c092b-1fa400-1768805c310c8; _hc.v=d204c0fc-a64a-6370-1019-977de0e17765.1615532308; edper=BmNx4PXTu2yskBqkpDXeM6ABgyrXEkHl6QtNRQF7MvwSnNLBicVOe-N945M-UGZSypZ3DVIiNxPSWCO0nsClOg; mpmerchant_portal_shopid=129383121; merchantBookShopID=129383121; merchantCategoryID=183; logan_custom_report=; platformSource=1; userPlatform=PC; requestSource=dp; realAccountId=54187346; accountSource=0; accountId=73686054; fromEntry=1; JSESSIONID=E3633C58D5CD8C6043C1778F383F4AB3; logan_session_token=ss49mk7coijqi0i7wumj; _lxsdk_s=178259b5023-a49-3e4-403%7C%7C76";
 public static String  readContentFromPost(String post_url,String postParm) throws IOException {
        // Post请求的url,与get不同的是不需要带参数
        URL postUrl = new URL(post_url);
        // 打开连接
        HttpURLConnection connection = (HttpURLConnection) postUrl
                .openConnection();
        // Output to the connection. Default is
        // false, set to true because post
        // method must write something to the
        // connection
        // 设置是否向connection输出,因为这个是post请求,参数要放在
        // http正文内,因此需要设为true
        connection.setDoOutput(true);
        // Read from the connection. Default is true.
        connection.setDoInput(true);
        // Set the post method. Default is GET
        connection.setRequestMethod("POST");
        // Post cannot use caches
        // Post 请求不能使用缓存
        connection.setUseCaches(false);

        connection.setInstanceFollowRedirects(true);

  

        connection.setRequestProperty("Accept", "*/*");
        connection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9");
        connection.setRequestProperty("Cache-Control", "no-cache");
        connection.setRequestProperty("Connection", "keep-alive");
        connection.setRequestProperty("Content-Length", postParm.length()+"");
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        connection.setRequestProperty("Cookie", cookie);
        connection.setRequestProperty("Host", "e.dianping.com");
        connection.setRequestProperty("Origin", "https://e.dianping.com");
        connection.setRequestProperty("Referer", "https://e.dianping.com/app/peon-hornet-promo/html/promo-list.html");
        connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
        connection.setRequestProperty("X-Requested-With", "XMLHttpRequest");



        connection.connect();
        DataOutputStream out = new DataOutputStream(connection
                .getOutputStream());

        String content = postParm;

        out.writeBytes(content);

        out.flush();
        out.close(); // flush and close
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));
        String result="";
        String line;
        while ((line = reader.readLine()) != null) {
         result=result+line;
        }
        reader.close();
        connection.disconnect();
  return result;
    }
 
 
 /**
  * 读取本地文件
  *
  * @param path
  *            文件所在路径 windows tmp\\json.txt
  * @return
  */
 public static String readFile(String path) {
  String rstr = "";
  try {
   // 读取文件
   FileReader fileReader = new FileReader(path);
   BufferedReader bufferedReader = new BufferedReader(fileReader);
   String str;
   // 读取多行文字
   while ((str = bufferedReader.readLine()) != null) {
    rstr += str;
   }

  } catch (Exception e) {
   e.printStackTrace();
  }
  return rstr;
 } 
 /**
  * 用key取出Map集合数据
  * @param map Map集合
  * @param key key值
  * @return Object对象
  */
 public static Object getMapValue(Map<String, Object> map,String key){
  Object value="";
  Set set = map.entrySet();//返回Map集合的映射视图
  Iterator it = map.entrySet().iterator();
  while (it.hasNext()) {
   Entry entry = (Entry) it.next();
   if (entry.getKey().equals(key)) {
    value=entry.getValue();
   }
  }
  return value;  
 } 

}

发表评论:

Powered By Z-BlogPHP 1.4 Deeplue Build 150101

Copyright Your WebSite.Some Rights Reserved.

蜀ICP备11021721号-5