简体   繁体   中英

JSON gson Parsing with \n

I have the following to code to parse some JSON:

public void parseAvatar() throws IOException, JSONException{    

    is.readInt();

    jo.put("currentHomeId", is.readLong());
    jo.put("homeId", is.readLong());

    byte isInClan = is.readByte();
    if (isInClan == 1) {
        jo.put("clanId", is.readLong());
        jo.put("clanName", is.readString());
        jo.put("clanBadge", is.readInt());
        jo.put("clanRole", is.readInt());
        jo.put("clanLevel", is.readInt());
    }

    byte a = is.readByte();
    if(a == 1){
        is.skipBytes(52);
    }else if(a == 0 && isInClan == 2){
        is.skipBytes(51);
    }else if(a == 0 && isInClan == 0){
        is.skipBytes(43);
    }else{
        is.skipBytes(44);
    }

    jo.put("league", is.readInt());
    jo.put("clanCaslteLevel", (is.readInt() + 1));
    jo.put("maxCcTroops", is.readInt());
    is.readInt();
    jo.put("townHall", (is.readInt() + 1));
    jo.put("userName", is.readString());
    is.readInt();
    jo.put("level", is.readInt());
    jo.put("exp", is.readInt());
    jo.put("gems", is.readInt());
    jo.put("freeGems", is.readInt());
    jo.put("Attack Rating", is.readInt());
    jo.put("Attack K Factor", is.readInt());
    jo.put("Trophies", is.readInt());
    jo.put("Attacks Won", is.readInt());
    jo.put("Attacks Lost", is.readInt());
    jo.put("Defenses Won", is.readInt());
    jo.put("Defenses Lost", is.readInt());

    is.readInt();
    is.readInt();
    is.readInt();
    boolean isPlayerTagAvailable = is.readBoolean();
    if (isPlayerTagAvailable) {
        jo.put("nameTag", is.readLong());
    }
    jo.put("userNameChange", is.readBoolean());
    jo.put("numOfNameChanges", is.readInt());
    jo.put("boughtGems", is.readInt());
    is.readInt();
    jo.put("inWar", is.readInt());

    Gson gson = new GsonBuilder().setPrettyPrinting().create();

    ArrayList<DataSlot> rcl = DataSlot.readDataSlots(is);
    String rcll = gson.toJson(rcl);
    jo.put("resourcesCapList",rcll);

    ArrayList<DataSlot> rl = DataSlot.readDataSlots(is);
    String rll = gson.toJson(rl);
    jo.put("resourcesList",rll);

}

Last two bits at the bottom should be producing their own array right?.

Here is the output (well part of it)

{
  "Trophies": 1262,
  "clanLevel": 1,
  "resourcesCapList": "[\n  {\n    \"value\": 2001000,\n    \"globalID\": 3000001\n  },\n  {\n    \"value\": 1251000,\n    \"globalID\": 3000002\n  },\n  {\n    \"value\": 0,\n    \"globalID\": 3000003\n  },\n  {\n    \"value\": 250000,\n    \"globalID\": 3000004\n  },\n  {\n    \"value\": 250000,\n    \"globalID\": 3000005\n  },\n  {\n    \"value\": 1000,\n    \"globalID\": 3000006\n  }\n]",
  "resourcesList": "[\n  {\n    \"value\": 0,\n    \"globalID\": 3000001\n  },\n  {\n    \"value\": 0,\n    \"globalID\": 3000002\n  }\n]",
  "Attack K Factor": -1653840941,
  "Attacks Won": 0,
  "freeGems": -2037996665,
  "clanBadge": 1526737746,
  "clanCaslteLevel": 2,
  "currentHomeId": 1,
  "clanRole": 2
}

Why is the resourceCapList and the resourceList so messed up? Shouldn't they print in a pretty way?

Also is there anyway I can keep the order of the elements the same?

Fixed :)

Thanks to blm for helping with this!

ArrayList<DataSlot> rcl = DataSlot.readDataSlots(is);
String rcll = gson.toJson(rcl);
JSONArray rcla = new JSONArray(rcll); 
jo.put("resourcesCapList",rcla)

I just converted the String into a JSONArray and it worked :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM