[英]JSON gson Parsing with \n
I have the following to code to parse some JSON: 我有以下代码来解析一些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? 为什么resourceCapList和resourceList如此混乱? 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! 感谢blm为您提供帮助!
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 :) 我只是将String转换为JSONArray,它就起作用了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.