您好我正在尝试创建一个巨大的json,因为它一直在最后添加双括号我似乎无法理解为什么它这样做我没有看到代码有什么问题
private static void DoLogin(UserData user) {
JSONObject UData = new JSONObject();
UData.put("name", user.CharName);
UData.put("intAccessLevel", 0);
UData.put("bitFounder", JSONObject.NULL);
UData.put("element", 1);
UData.put("idPlayer", user.UserId);
UData.put("intGold", user.Gold);
UData.put("intGem", user.Gem);
UData.put("intGemLifetime", JSONObject.NULL);
UData.put("intAlignment", 0);
UData.put("intAP", 0);
UData.put("intBlock", 0);
UData.put("intChaos", 0);
UData.put("intCriticalHit" , 0);
UData.put("intDP", 0);
UData.put("intEarth", 0);
UData.put("intEnergy", 0);
UData.put("intFire", 0);
UData.put("intHits", 0);
UData.put("intHometown" , 1);
UData.put("intIce", 0);
UData.put("intInitiative", 0);
UData.put("intLuck", 0);
UData.put("intNeutral", 0);
UData.put("intPower" ,0);
UData.put("intStamina", 0);
UData.put("intShadow", 0);
UData.put("intWater", 0);
UData.put("dateDOB", "1/1/1987");
UData.put("dateMuteUntil", JSONObject.NULL);
if (user.CharactersID.split(",").length > 0) {
JSONArray CharactersArray = new JSONArray();
JSONArray CharInven = new JSONArray();
for (String id : user.CharactersID.split(",")) {
if (DBGet.CharacterList.containsKey(Integer.parseInt(id))) {
CharactersArray.put(Getter.GetCharacterData(DBGet.CharacterList.get(Integer.parseInt(id))));
Characters character = DBGet.CharacterList.get(Integer.parseInt(id));
JSONObject charInvenData = new JSONObject();
charInvenData.put("idAdjPlayerCharacter", character.Id);
charInvenData.put("idPlayer", user.UserId);
charInvenData.put("idCharacter", character.Id);
charInvenData.put("intUnlocked", 1);
charInvenData.put("intLevel", character.Level);
charInvenData.put("intExp", 0);
if (user.CharID == character.Id) {
charInvenData.put("bDefault", true);
} else {
charInvenData.put("bDefault", false);
}
CharInven.put(charInvenData);
}
}
JSONObject cmd = new JSONObject();
cmd.put("status", 1);
cmd.put("type", "login");
cmd.put("player", UData);
cmd.put("characters", CharactersArray);
cmd.put("charInventory", CharInven);
cmd.put("items", new JSONArray());
cmd.put("itemInventory", new JSONArray());
cmd.put("cards", JSONObject.NULL);
cmd.put("cardInventory", JSONObject.NULL);
user.SendPacket(cmd.toString());
}
}
Getter.GetCharacterData是什么:
public static JSONObject GetCharacterData(Characters Character) {
JSONObject characterData = new JSONObject();
characterData.put("idCharacter",Character.Id);
characterData.put("strName", Character.Name);
characterData.put("idCharacterType", Character.CharacterType);
characterData.put("idCharacterPrereq", Character.CharacterPrereq);
characterData.put("idCharacterOwner", Character.Id);
characterData.put("idItemPrereq", Character.ItemPrereq);
characterData.put("strFilename", Character.Filename);
characterData.put("strAttach", Character.Attach);
characterData.put("idElement", Character.Element);
characterData.put("intCaptureRate", Character.CaptureRate);
characterData.put("intCost", Character.Cost);
characterData.put("intLevel", Character.Level);
characterData.put("intAlignment", JSONObject.NULL);
characterData.put("intStoreGold", JSONObject.NULL);
characterData.put("intStoreGem", JSONObject.NULL);
characterData.put("bSellable", JSONObject.NULL);
return characterData;
}
我需要json结果:
{
"status": 1,
"type": "login",
"player": {
"name": "What",
"intAccessLevel": 0,
"bitFounder": null,
"element": 1,
"idPlayer": 4,
"intGold": 0,
"intGem": 0,
"intGemLifetime": null,
"intAlignment": 0,
"intAP": 0,
"intBlock": 0,
"intChaos": 0,
"intCriticalHit": 0,
"intDP": 0,
"intEarth": 0,
"intEnergy": 0,
"intFire": 0,
"intHits": 0,
"intHometown": 1,
"intIce": 0,
"intInitiative": 0,
"intLuck": 0,
"intNeutral": 0,
"intPower": 0,
"intStamina": 0,
"intShadow": 0,
"intWater": 0,
"dateDOB": "1/1/1987",
"dateMuteUntil": null
},
"characters": [
{
"idCharacter": 264,
"strName": "Infernal Fiend",
"idCharacterType": 3,
"idCharacterPrereq": -1,
"idCharacterOwner": 264,
"idItemPrereq": -1,
"strFilename": "InfernalFiendSwords1-Fire-E-M-Master1",
"strAttach": -1,
"idElement": 1,
"intCaptureRate": 25,
"intCost": 0,
"intLevel": 9,
"intAlignment": null,
"intStoreGold": null,
"intStoreGem": null,
"bSellable": null
},
{
"idCharacter": 4,
"strName": "Void of Nulgath",
"idCharacterType": 1,
"idCharacterPrereq": -1,
"idCharacterOwner": 4,
"idItemPrereq": -1,
"strFilename": "0VoidKnightArmblades1-Shadow-N-M-Starter1-opt",
"strAttach": -1,
"idElement": 7,
"intCaptureRate": 50,
"intCost": 0,
"intLevel": 0,
"intAlignment": null,
"intStoreGold": null,
"intStoreGem": null,
"bSellable": null
},
{
"idCharacter": 5,
"strName": "Wolf",
"idCharacterType": 1,
"idCharacterPrereq": -1,
"idCharacterOwner": 5,
"idItemPrereq": -1,
"strFilename": "0Wolf1-Lighting-N-M-Starter1-opt",
"strAttach": -1,
"idElement": 3,
"intCaptureRate": 50,
"intCost": 0,
"intLevel": 0,
"intAlignment": null,
"intStoreGold": null,
"intStoreGem": null,
"bSellable": null
}
],
"charInventory": [
{
"idAdjPlayerCharacter": 264,
"idPlayer": 4,
"idCharacter": 264,
"intUnlocked": 1,
"intLevel": 9,
"intExp": 0,
"bDefault": false
},
{
"idAdjPlayerCharacter": 4,
"idPlayer": 4,
"idCharacter": 4,
"intUnlocked": 1,
"intLevel": 0,
"intExp": 0,
"bDefault": false
},
{
"idAdjPlayerCharacter": 5,
"idPlayer": 4,
"idCharacter": 5,
"intUnlocked": 1,
"intLevel": 0,
"intExp": 0,
"bDefault": true
}
],
"items": [],
"itemInventory": [],
"cards": null,
"cardInventory": null
}
我的代码返回的内容:
{
"characters": [
{
"idElement": 1,
"idCharacter": 264,
"strAttach": -1,
"intLevel": 9,
"intAlignment": null,
"intStoreGem": null,
"strFilename": "InfernalFiendSwords1-Fire-E-M-Master1",
"idItemPrereq": -1,
"intCaptureRate": 25,
"strName": "Infernal Fiend",
"idCharacterType": 3,
"intCost": 0,
"bSellable": null,
"idCharacterOwner": 264,
"intStoreGold": null,
"idCharacterPrereq": -1
},
{
"idElement": 7,
"idCharacter": 4,
"strAttach": -1,
"intLevel": 0,
"intAlignment": null,
"intStoreGem": null,
"strFilename": "0VoidKnightArmblades1-Shadow-N-M-Starter1-opt",
"idItemPrereq": -1,
"intCaptureRate": 50,
"strName": "Void of Nulgath",
"idCharacterType": 1,
"intCost": 0,
"bSellable": null,
"idCharacterOwner": 4,
"intStoreGold": null,
"idCharacterPrereq": -1
},
{
"idElement": 3,
"idCharacter": 5,
"strAttach": -1,
"intLevel": 0,
"intAlignment": null,
"intStoreGem": null,
"strFilename": "0Wolf1-Lighting-N-M-Starter1-opt",
"idItemPrereq": -1,
"intCaptureRate": 50,
"strName": "Wolf",
"idCharacterType": 1,
"intCost": 0,
"bSellable": null,
"idCharacterOwner": 5,
"intStoreGold": null,
"idCharacterPrereq": -1
}
],
"charInventory": [
{
"bDefault": false,
"idPlayer": 4,
"idCharacter": 264,
"intUnlocked": 1,
"idAdjPlayerCharacter": 264,
"intLevel": 9,
"intExp": 0
},
{
"bDefault": false,
"idPlayer": 4,
"idCharacter": 4,
"intUnlocked": 1,
"idAdjPlayerCharacter": 4,
"intLevel": 0,
"intExp": 0
},
{
"bDefault": true,
"idPlayer": 4,
"idCharacter": 5,
"intUnlocked": 1,
"idAdjPlayerCharacter": 5,
"intLevel": 0,
"intExp": 0
}
],
"cards": null,
"itemInventory": [],
"cardInventory": null,
"type": "login",
"items": [],
"status": 1,
"player": {
"intAccessLevel": 0,
"intDP": 0,
"intPower": 0,
"intShadow": 0,
"intGold": 0,
"intChaos": 0,
"intHometown": 1,
"intAlignment": 0,
"intGem": 0,
"intBlock": 0,
"dateMuteUntil": null,
"intCriticalHit": 0,
"intWater": 0,
"bitFounder": null,
"element": 1,
"intGemLifetime": null,
"intFire": 0,
"intAP": 0,
"intHits": 0,
"intLuck": 0,
"intIce": 0,
"intEarth": 0,
"intInitiative": 0,
"intNeutral": 0,
"dateDOB": "1/1/1987",
"intStamina": 0,
"idPlayer": 4,
"intEnergy": 0,
"name": "What"
}
}
为什么这样做可以让任何人做出修复或者尝试帮助非常感谢你。
答案 0 :(得分:3)
除了元素顺序之外,您的期望值和输出值之间没有区别。
答案 1 :(得分:0)
第一个"}"是为了对象玩家。我认为你的JSON是正确的!