如何将HTTP响应主体解析为Java中的表?

时间:2015-08-20 08:52:45

标签: java json

例如,我收到了回复。

Response Code : 200
[1,
[1,
[{
    "uid": "2400065",
    "state": "1",
    "uname": "Chaddy",
    "ulv": "113",
    "ujob": "1",
    "sex": "1",
    "zhanli": "3734099",
    "tempscore": "0",
    "score": "88721",
    "pos": "1",
    "totalscore": "283551",
    "tsleave": "23976"
},
{
    "uid": "2400072",
    "state": "100",
    "uname": "Defqon",
    "ulv": "115",
    "ujob": "2",
    "sex": "1",
    "zhanli": "3713736",
    "tempscore": "0",
    "score": "125948",
    "pos": "2",
    "totalscore": "806093",
    "tsleave": "27127"
},
{
    "uid": "2400092",
    "state": "1",
    "uname": "-T-",
    "ulv": "115",
    "ujob": "2",
    "sex": "1",
    "zhanli": "3580389",
    "tempscore": "0",
    "score": "8266",
    "pos": "2",
    "totalscore": "182526",
    "tsleave": "1592"
},
{
    "uid": "2401220",
    "state": "1",
    "uname": "FENRIZ",
    "ulv": "102",
    "ujob": "1",
    "sex": "1",
    "zhanli": "1674646",
    "tempscore": "0",
    "score": "939",
    "pos": "1",
    "totalscore": "112134",
    "tsleave": "56424"
},
{
    "uid": "2401982",
    "state": "1",
    "uname": "Leela",
    "ulv": "99",
    "ujob": "3",
    "sex": "2",
    "zhanli": "1004770",
    "tempscore": "0",
    "score": "1178",
    "pos": "1",
    "totalscore": "30123",
    "tsleave": "5477"
},
{
    "uid": "2402027",
    "state": "1000",
    "uname": "Terran",
    "ulv": "115",
    "ujob": "2",
    "sex": "2",
    "zhanli": "5286987",
    "tempscore": "0",
    "score": "9464",
    "pos": "2",
    "totalscore": "1242654",
    "tsleave": "-55"
},
{
    "uid": "2403600",
    "state": "1",
    "uname": "Doombraker",
    "ulv": "104",
    "ujob": "2",
    "sex": "1",
    "zhanli": "20040",
    "tempscore": "0",
    "score": "2137",
    "pos": "1",
    "totalscore": "44097",
    "tsleave": "1478"
},
{
    "uid": "2405258",
    "state": "1",
    "uname": "Spectral",
    "ulv": "110",
    "ujob": "3",
    "sex": "1",
    "zhanli": "2693099",
    "tempscore": "0",
    "score": "9928",
    "pos": "2",
    "totalscore": "94438",
    "tsleave": "50506"
},
{
    "uid": "2405842",
    "state": "1",
    "uname": "Litche",
    "ulv": "108",
    "ujob": "3",
    "sex": "2",
    "zhanli": "1891758",
    "tempscore": "0",
    "score": "31237",
    "pos": "1",
    "totalscore": "377532",
    "tsleave": "24500"
},
{
    "uid": "2406113",
    "state": "1",
    "uname": "CutThroat",
    "ulv": "114",
    "ujob": "3",
    "sex": "1",
    "zhanli": "5773150",
    "tempscore": "0",
    "score": "130",
    "pos": "2",
    "totalscore": "130",
    "tsleave": "16106"
},
{
    "uid": "2410034",
    "state": "1",
    "uname": "Varlet",
    "ulv": "104",
    "ujob": "2",
    "sex": "2",
    "zhanli": "2052631",
    "tempscore": "0",
    "score": "412",
    "pos": "2",
    "totalscore": "55352",
    "tsleave": "112"
},
{
    "uid": "2411462",
    "state": "1",
    "uname": "Mace",
    "ulv": "104",
    "ujob": "3",
    "sex": "1",
    "zhanli": "1066823",
    "tempscore": "0",
    "score": "771",
    "pos": "1",
    "totalscore": "32661",
    "tsleave": "-40"
},
{
    "uid": "2411775",
    "state": "100",
    "uname": "iTzAim",
    "ulv": "108",
    "ujob": "2",
    "sex": "2",
    "zhanli": "2827787",
    "tempscore": "0",
    "score": "55405",
    "pos": "2",
    "totalscore": "315505",
    "tsleave": "35730"
},
{
    "uid": "2412028",
    "state": "100",
    "uname": "zeebaah ",
    "ulv": "107",
    "ujob": "2",
    "sex": "2",
    "zhanli": "2598159",
    "tempscore": "0",
    "score": "1069",
    "pos": "2",
    "totalscore": "400519",
    "tsleave": "8597"
},
{
    "uid": "2412767",
    "state": "1",
    "uname": "Craxy",
    "ulv": "114",
    "ujob": "3",
    "sex": "2",
    "zhanli": "3728911",
    "tempscore": "0",
    "score": "48469",
    "pos": "2",
    "totalscore": "958214",
    "tsleave": "11114"
},
{
    "uid": "2413680",
    "state": "1",
    "uname": "Sebbsco",
    "ulv": "111",
    "ujob": "2",
    "sex": "1",
    "zhanli": "2700567",
    "tempscore": "0",
    "score": "879",
    "pos": "2",
    "totalscore": "439664",
    "tsleave": "45336"
},
{
    "uid": "2414561",
    "state": "1",
    "uname": "Inburst",
    "ulv": "114",
    "ujob": "3",
    "sex": "1",
    "zhanli": "3230275",
    "tempscore": "0",
    "score": "5680",
    "pos": "2",
    "totalscore": "123340",
    "tsleave": "1993"
},
{
    "uid": "2414562",
    "state": "1",
    "uname": "Fairy",
    "ulv": "105",
    "ujob": "2",
    "sex": "2",
    "zhanli": "1529066",
    "tempscore": "0",
    "score": "15826",
    "pos": "1",
    "totalscore": "111246",
    "tsleave": "1264"
},
{
    "uid": "2414601",
    "state": "1",
    "uname": "Arsch Nase",
    "ulv": "108",
    "ujob": "1",
    "sex": "1",
    "zhanli": "2213510",
    "tempscore": "0",
    "score": "2132",
    "pos": "1",
    "totalscore": "236652",
    "tsleave": "347"
},
{
    "uid": "2414674",
    "state": "1",
    "uname": "Ginomania",
    "ulv": "109",
    "ujob": "2",
    "sex": "1",
    "zhanli": "2177489",
    "tempscore": "0",
    "score": "17342",
    "pos": "2",
    "totalscore": "245357",
    "tsleave": "93841"
},
{
    "uid": "2414906",
    "state": "100",
    "uname": "NaughtyXXX",
    "ulv": "118",
    "ujob": "3",
    "sex": "2",
    "zhanli": "6050126",
    "tempscore": "0",
    "score": "2880",
    "pos": "2",
    "totalscore": "1598775",
    "tsleave": "20434"
},
{
    "uid": "2416506",
    "state": "1",
    "uname": "Stampo133",
    "ulv": "107",
    "ujob": "2",
    "sex": "1",
    "zhanli": "1682759",
    "tempscore": "0",
    "score": "2857",
    "pos": "1",
    "totalscore": "25602",
    "tsleave": "35109"
},
{
    "uid": "2417432",
    "state": "1",
    "uname": "Mokkete",
    "ulv": "101",
    "ujob": "1",
    "sex": "2",
    "zhanli": "1046358",
    "tempscore": "0",
    "score": "1371",
    "pos": "1",
    "totalscore": "103376",
    "tsleave": "19759"
},
{
    "uid": "2417626",
    "state": "1",
    "uname": "Zup",
    "ulv": "102",
    "ujob": "1",
    "sex": "1",
    "zhanli": "1591753",
    "tempscore": "0",
    "score": "609",
    "pos": "1",
    "totalscore": "214939",
    "tsleave": "14554"
},
{
    "uid": "2417680",
    "state": "1",
    "uname": "Dardanos",
    "ulv": "102",
    "ujob": "3",
    "sex": "1",
    "zhanli": "1208833",
    "tempscore": "0",
    "score": "6046",
    "pos": "1",
    "totalscore": "94306",
    "tsleave": "213630"
},
{
    "uid": "2418935",
    "state": "1",
    "uname": "God",
    "ulv": "103",
    "ujob": "1",
    "sex": "2",
    "zhanli": "1688358",
    "tempscore": "0",
    "score": "1852",
    "pos": "0",
    "totalscore": "20852",
    "tsleave": "24683"
},
{
    "uid": "2419001",
    "state": "1",
    "uname": "DeadEye",
    "ulv": "102",
    "ujob": "2",
    "sex": "1",
    "zhanli": "1571285",
    "tempscore": "0",
    "score": "12305",
    "pos": "1",
    "totalscore": "70175",
    "tsleave": "45189"
},
{
    "uid": "2419659",
    "state": "1",
    "uname": "Tahari",
    "ulv": "107",
    "ujob": "2",
    "sex": "2",
    "zhanli": "2057478",
    "tempscore": "0",
    "score": "39848",
    "pos": "2",
    "totalscore": "129238",
    "tsleave": "17227"
},
{
    "uid": "2420095",
    "state": "1",
    "uname": "GEORGERM",
    "ulv": "107",
    "ujob": "2",
    "sex": "1",
    "zhanli": "3495294",
    "tempscore": "0",
    "score": "1007",
    "pos": "2",
    "totalscore": "1001887",
    "tsleave": "8997"
},
{
    "uid": "2420302",
    "state": "1",
    "uname": "Madias",
    "ulv": "107",
    "ujob": "3",
    "sex": "2",
    "zhanli": "2028453",
    "tempscore": "0",
    "score": "3831",
    "pos": "1",
    "totalscore": "157286",
    "tsleave": "1611"
},
{
    "uid": "2422392",
    "state": "1",
    "uname": "bobusan",
    "ulv": "103",
    "ujob": "1",
    "sex": "1",
    "zhanli": "1515677",
    "tempscore": "0",
    "score": "6588",
    "pos": "1",
    "totalscore": "87048",
    "tsleave": "19968"
},
{
    "uid": "2423812",
    "state": "1",
    "uname": "Lenadas",
    "ulv": "111",
    "ujob": "2",
    "sex": "1",
    "zhanli": "3309340",
    "tempscore": "0",
    "score": "40409",
    "pos": "2",
    "totalscore": "303229",
    "tsleave": "23333"
},
{
    "uid": "2424648",
    "state": "1",
    "uname": "Hawk",
    "ulv": "104",
    "ujob": "2",
    "sex": "2",
    "zhanli": "1488922",
    "tempscore": "0",
    "score": "2549",
    "pos": "1",
    "totalscore": "82279",
    "tsleave": "13027"
},
{
    "uid": "2425107",
    "state": "1",
    "uname": "Viktis",
    "ulv": "109",
    "ujob": "3",
    "sex": "1",
    "zhanli": "2005612",
    "tempscore": "0",
    "score": "32616",
    "pos": "1",
    "totalscore": "190616",
    "tsleave": "10434"
},
{
    "uid": "2425616",
    "state": "1",
    "uname": "Bluffpoint",
    "ulv": "103",
    "ujob": "1",
    "sex": "2",
    "zhanli": "2003901",
    "tempscore": "0",
    "score": "4375",
    "pos": "1",
    "totalscore": "276225",
    "tsleave": "2730"
},
{
    "uid": "2425879",
    "state": "100",
    "uname": "Tess",
    "ulv": "104",
    "ujob": "3",
    "sex": "2",
    "zhanli": "1076686",
    "tempscore": "0",
    "score": "5727",
    "pos": "1",
    "totalscore": "155062",
    "tsleave": "75636"
},
{
    "uid": "2426584",
    "state": "1",
    "uname": "Tinoc",
    "ulv": "105",
    "ujob": "3",
    "sex": "1",
    "zhanli": "1264327",
    "tempscore": "0",
    "score": "737",
    "pos": "1",
    "totalscore": "87857",
    "tsleave": "4732"
},
{
    "uid": "2428502",
    "state": "1",
    "uname": "DragonHawk",
    "ulv": "82",
    "ujob": "2",
    "sex": "2",
    "zhanli": "172406",
    "tempscore": "0",
    "score": "13646",
    "pos": "1",
    "totalscore": "13646",
    "tsleave": "115154"
}]]]

我如何将其解析为表格。我已经尝试了一些解析器示例,但我不能正确地进行。我需要在每个uid之前进行行分割:

我已经迷失了几天。

1 个答案:

答案 0 :(得分:0)

您可以使用Gson来解析响应,但是您需要先清除未使用的内容并将其包装到JSON对象中。我使用BufferedReader来读取上面的响应字符串。提取后,您可以使用对象关系映射器来存储HttpResponse对象。 它可能看起来像那样:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;

public class Answers {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("Json.txt"));
        String line;
        StringBuilder json = new StringBuilder();
        json.append("{\n\"");
        line = br.readLine();
        json.append(line.replace(" ", "").replace(":", "\":")
                + ", \n\"Content\":");
        while ((line = br.readLine()) != null) {
            if (!line.matches("\\[[0-1],"))
                if (!line.matches("\\}\\]\\]\\]"))
                    json.append(line.replace(" ", "") + "\n");
                else {
                    json.append("}]\n");
                }

        }
        json.append("}");
        String jsonstr = json.toString();
        Gson gson = new Gson();
        HttpResponse myModel = (HttpResponse) gson.fromJson(jsonstr, HttpResponse.class);
        System.out.println(gson.toJson(myModel));
        br.close();

    }

    class HttpResponse {

        @SerializedName("ResponseCode")
        private int responseCode;
        @SerializedName("Content")
        private List<User> content;


        class User {
            private String uid;
            private String state;
            private String uname;
            private String ulv;
            private String ujob;
            private String sex;
            private String zhanli;
            private String tempscore;
            private String score;
            private String pos;
            private String totalscore;
            private String tsleave;
        }

    }

}

输出:

{"ResponseCode":200,"Content":[{"uid":"2400065","state":"1","uname":"Chaddy","ulv":"113","ujob":"1","sex":"1","zhanli":"3734099","tempscore":"0","score":"88721","pos":"1","totalscore":"283551","tsleave":"23976"},{"uid":"2400072","state":"100","uname":"Defqon","ulv":"115","ujob":"2","sex":"1","zhanli":"3713736","tempscore":"0","score":"125948","pos":"2","totalscore":"806093","tsleave":"27127"},{"uid":"2400092","state":"1","uname":"-T-","ulv":"115","ujob":"2","sex":"1","zhanli":"3580389","tempscore":"0","score":"8266","pos":"2","totalscore":"182526","tsleave":"1592"},{"uid":"2401220","state":"1","uname":"FENRIZ","ulv":"102","ujob":"1","sex":"1","zhanli":"1674646","tempscore":"0","score":"939","pos":"1","totalscore":"112134","tsleave":"56424"},{"uid":"2401982","state":"1","uname":"Leela","ulv":"99","ujob":"3","sex":"2","zhanli":"1004770","tempscore":"0","score":"1178","pos":"1","totalscore":"30123","tsleave":"5477"},{"uid":"2402027","state":"1000","uname":"Terran","ulv":"115","ujob":"2","sex":"2","zhanli":"5286987","tempscore":"0","score":"9464","pos":"2","totalscore":"1242654","tsleave":"-55"},{"uid":"2403600","state":"1","uname":"Doombraker","ulv":"104","ujob":"2","sex":"1","zhanli":"20040","tempscore":"0","score":"2137","pos":"1","totalscore":"44097","tsleave":"1478"},{"uid":"2405258","state":"1","uname":"Spectral","ulv":"110","ujob":"3","sex":"1","zhanli":"2693099","tempscore":"0","score":"9928","pos":"2","totalscore":"94438","tsleave":"50506"},{"uid":"2405842","state":"1","uname":"Litche","ulv":"108","ujob":"3","sex":"2","zhanli":"1891758","tempscore":"0","score":"31237","pos":"1","totalscore":"377532","tsleave":"24500"},{"uid":"2406113","state":"1","uname":"CutThroat","ulv":"114","ujob":"3","sex":"1","zhanli":"5773150","tempscore":"0","score":"130","pos":"2","totalscore":"130","tsleave":"16106"},{"uid":"2410034","state":"1","uname":"Varlet","ulv":"104","ujob":"2","sex":"2","zhanli":"2052631","tempscore":"0","score":"412","pos":"2","totalscore":"55352","tsleave":"112"},{"uid":"2411462","state":"1","uname":"Mace","ulv":"104","ujob":"3","sex":"1","zhanli":"1066823","tempscore":"0","score":"771","pos":"1","totalscore":"32661","tsleave":"-40"},{"uid":"2411775","state":"100","uname":"iTzAim","ulv":"108","ujob":"2","sex":"2","zhanli":"2827787","tempscore":"0","score":"55405","pos":"2","totalscore":"315505","tsleave":"35730"},{"uid":"2412028","state":"100","uname":"zeebaah","ulv":"107","ujob":"2","sex":"2","zhanli":"2598159","tempscore":"0","score":"1069","pos":"2","totalscore":"400519","tsleave":"8597"},{"uid":"2412767","state":"1","uname":"Craxy","ulv":"114","ujob":"3","sex":"2","zhanli":"3728911","tempscore":"0","score":"48469","pos":"2","totalscore":"958214","tsleave":"11114"},{"uid":"2413680","state":"1","uname":"Sebbsco","ulv":"111","ujob":"2","sex":"1","zhanli":"2700567","tempscore":"0","score":"879","pos":"2","totalscore":"439664","tsleave":"45336"},{"uid":"2414561","state":"1","uname":"Inburst","ulv":"114","ujob":"3","sex":"1","zhanli":"3230275","tempscore":"0","score":"5680","pos":"2","totalscore":"123340","tsleave":"1993"},{"uid":"2414562","state":"1","uname":"Fairy","ulv":"105","ujob":"2","sex":"2","zhanli":"1529066","tempscore":"0","score":"15826","pos":"1","totalscore":"111246","tsleave":"1264"},{"uid":"2414601","state":"1","uname":"ArschNase","ulv":"108","ujob":"1","sex":"1","zhanli":"2213510","tempscore":"0","score":"2132","pos":"1","totalscore":"236652","tsleave":"347"},{"uid":"2414674","state":"1","uname":"Ginomania","ulv":"109","ujob":"2","sex":"1","zhanli":"2177489","tempscore":"0","score":"17342","pos":"2","totalscore":"245357","tsleave":"93841"},{"uid":"2414906","state":"100","uname":"NaughtyXXX","ulv":"118","ujob":"3","sex":"2","zhanli":"6050126","tempscore":"0","score":"2880","pos":"2","totalscore":"1598775","tsleave":"20434"},{"uid":"2416506","state":"1","uname":"Stampo133","ulv":"107","ujob":"2","sex":"1","zhanli":"1682759","tempscore":"0","score":"2857","pos":"1","totalscore":"25602","tsleave":"35109"},{"uid":"2417432","state":"1","uname":"Mokkete","ulv":"101","ujob":"1","sex":"2","zhanli":"1046358","tempscore":"0","score":"1371","pos":"1","totalscore":"103376","tsleave":"19759"},{"uid":"2417626","state":"1","uname":"Zup","ulv":"102","ujob":"1","sex":"1","zhanli":"1591753","tempscore":"0","score":"609","pos":"1","totalscore":"214939","tsleave":"14554"},{"uid":"2417680","state":"1","uname":"Dardanos","ulv":"102","ujob":"3","sex":"1","zhanli":"1208833","tempscore":"0","score":"6046","pos":"1","totalscore":"94306","tsleave":"213630"},{"uid":"2418935","state":"1","uname":"God","ulv":"103","ujob":"1","sex":"2","zhanli":"1688358","tempscore":"0","score":"1852","pos":"0","totalscore":"20852","tsleave":"24683"},{"uid":"2419001","state":"1","uname":"DeadEye","ulv":"102","ujob":"2","sex":"1","zhanli":"1571285","tempscore":"0","score":"12305","pos":"1","totalscore":"70175","tsleave":"45189"},{"uid":"2419659","state":"1","uname":"Tahari","ulv":"107","ujob":"2","sex":"2","zhanli":"2057478","tempscore":"0","score":"39848","pos":"2","totalscore":"129238","tsleave":"17227"},{"uid":"2420095","state":"1","uname":"GEORGERM","ulv":"107","ujob":"2","sex":"1","zhanli":"3495294","tempscore":"0","score":"1007","pos":"2","totalscore":"1001887","tsleave":"8997"},{"uid":"2420302","state":"1","uname":"Madias","ulv":"107","ujob":"3","sex":"2","zhanli":"2028453","tempscore":"0","score":"3831","pos":"1","totalscore":"157286","tsleave":"1611"},{"uid":"2422392","state":"1","uname":"bobusan","ulv":"103","ujob":"1","sex":"1","zhanli":"1515677","tempscore":"0","score":"6588","pos":"1","totalscore":"87048","tsleave":"19968"},{"uid":"2423812","state":"1","uname":"Lenadas","ulv":"111","ujob":"2","sex":"1","zhanli":"3309340","tempscore":"0","score":"40409","pos":"2","totalscore":"303229","tsleave":"23333"},{"uid":"2424648","state":"1","uname":"Hawk","ulv":"104","ujob":"2","sex":"2","zhanli":"1488922","tempscore":"0","score":"2549","pos":"1","totalscore":"82279","tsleave":"13027"},{"uid":"2425107","state":"1","uname":"Viktis","ulv":"109","ujob":"3","sex":"1","zhanli":"2005612","tempscore":"0","score":"32616","pos":"1","totalscore":"190616","tsleave":"10434"},{"uid":"2425616","state":"1","uname":"Bluffpoint","ulv":"103","ujob":"1","sex":"2","zhanli":"2003901","tempscore":"0","score":"4375","pos":"1","totalscore":"276225","tsleave":"2730"},{"uid":"2425879","state":"100","uname":"Tess","ulv":"104","ujob":"3","sex":"2","zhanli":"1076686","tempscore":"0","score":"5727","pos":"1","totalscore":"155062","tsleave":"75636"},{"uid":"2426584","state":"1","uname":"Tinoc","ulv":"105","ujob":"3","sex":"1","zhanli":"1264327","tempscore":"0","score":"737","pos":"1","totalscore":"87857","tsleave":"4732"},{"uid":"2428502","state":"1","uname":"DragonHawk","ulv":"82","ujob":"2","sex":"2","zhanli":"172406","tempscore":"0","score":"13646","pos":"1","totalscore":"13646","tsleave":"115154"}]}