链接获取Json字符串后遍历

来源:互联网 发布:电子小报制作软件 编辑:程序博客网 时间:2024/06/08 19:21
package test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Test {
public static void main(String[] args) {
    branchProfit();
}
public static String loadJSON (String url) {
    StringBuilder json = new StringBuilder();
    try {
        URL oracle = new URL(url);
        URLConnection yc = oracle.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                                    yc.getInputStream(),"UTF-8"));
        String inputLine = null;
        while ( (inputLine = in.readLine()) != null) {
            json.append(inputLine);
        }
        in.close();
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    }
    return json.toString();
}

public static void branchProfit(){
    String platform = loadJSON("http://192.168.100.3:8080/api/agentinfo/getFinanceInfo.html?id=562");
    String merchant = loadJSON("http://192.168.100.3:8080/api/merchantinfo/getFinanceInfo.html?id=100464");
    JSONObject dev=JSONObject.fromObject(platform);
    JSONArray array=JSONArray.fromObject(dev.get("data"));//获取json对象的data
    JSONObject obj=JSONObject.fromObject(array.get(0));//获取json数组的第一个
    JSONArray dArray = (JSONArray) obj.get("data");
    for (int i = 0; i < dArray.size(); i++) {
        JSONObject jsonObj = dArray.getJSONObject(i);
        System.out.println(jsonObj.get("agentAlipayName"));
        System.out.println(jsonObj.get("agentName"));
        System.out.println(jsonObj.get("agentId"));
        System.out.println(jsonObj.get("agentPayOdd"));
        System.out.println(jsonObj.get("agentCouponsOdd"));
        System.out.println(jsonObj.get("agentAlipay"));
        }
    System.out.println(dArray);
}
}

0 0