0

在发布之前,我已经在 StackOverflow 上搜索并尝试了所有示例。似乎没有任何效果,所以希望有人可以对正在发生的事情有所了解。

我有一个从 UTF-8 格式的 WCF 服务返回的 json。当数据返回时,我对数据进行按摩以删除 '\n' (由 BufferedReader 附加到 StringBuilder 引起)和反斜杠(如果它们存在并留下此字符串):

(from Log.d):
D/WEB: "{\"BattleList\":[{\"LiveID\":1,\"BattleID\":1,\"OvxyzID\":1234,\"ChallengerID\":1,\"OpponentID\":2,\"ChallengerName\":\"Tank1\",\"OpponentName\":\"Tank2\",\"TimeChallenged\":\"12-6-2015\",\"IsActive\":1,\"TimeEnded\":\"0001-01-01T00:00:00\"}]}"

我已经尝试了所有主要的 json 解析器来让它工作:gson、json-simple、json-lib、minimal-json。所有结果都相同“无法转换为 JSONObject”或“不是有效数组”或“无效值”等。

一个确切的 org.json 异常:

org.json.JSONException: Value {"BattleList":[{"LiveID":1,"BattleID":1,"OvxyzID":1234,"ChallengerID":1,"OpponentID":2,"ChallengerName":"Tank1","OpponentName":"Tank2","TimeChallenged":"12-6-2015","IsActive":1,"TimeEnded":"0001-01-01T00:00:00"}]} of type java.lang.String cannot be converted to JSONObject
12-12 02:56:12.138 27304-27304/com.myproject W/System.err:     
at org.json.JSON.typeMismatch(JSON.java:111)

即使异常是 typeMismatch 我检查以确保字段名称及其各自的数据类型相同。

但是,在所有这些情况下,如果我复制字符串并将其放入字符串变量 String json = "{\"BattleList\":[{\"LiveID\":1,\"BattleID\":1,\ "OvxyzID\":1234,\"ChallengerID\":1,\"OpponentID\":2,\"ChallengerName\":\"Tank1\",\"OpponentName\":\"Tank2\",\"TimeChallenged \":\"12-6-2015\",\"IsActive\":1,\"TimeEnded\":\"0001-01-01T00:00:00\"}]}"

org.json 能够从变量中创建一个有效的对象,但是将它与我​​的“结果”变量相匹配。它是相同的。

我已经在线访问了 json 验证器并使用了我的 Notepad++ json 插件,并且我的返回结果是有效的。

使用 Gson,我创建了一个自定义反序列化器,我发现有趣的是我的返回结果 JsonElement 只获取'{'并引发异常。当我使用 String 变量时,JsonElement 会获取整个字符串。

以下是我尝试反序列化的不同方法。如果信息太多且不需要,我深表歉意。

//json-lib
//net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(response);

//json-simple
//JSONParser parser = new JSONParser();
//org.json.simple.JSONObject json = (org.json.simple.JSONObject)parser.parse(response);
//org.json.simple.JSONObject obj = (org.json.simple.JSONObject) JSONValue.parse(response);
//JSONArray jsonArray = new JSONArray(response);
//JSONObject json = (JSONObject) new JSONParser().parse(response);


//org.json
//JSONObject jsonObj = null;
//try {
//    jsonObj = new JSONObject(json);
//} catch (org.json.JSONException e) {
//    e.printStackTrace();
//}
//JSONObject jObj = new JSONObject(response);
//JSONObject jobj = new JSONObject(response);
//JSONArray jarray = jobj.getJSONArray("BattleList");


//Gson
//String json = "{\"BattleList\":[{\"LiveID\":1,\"BattleID\":1,\"OvxyzID\":1234,\"ChallengerID\":1,\"OpponentID\":2,\"ChallengerName\":\"Tank1\",\"OpponentName\":\"Tank2\",\"TimeChallenged\":\"12-6-2015\",\"IsActive\":1,\"TimeEnded\":\"0001-01-01T00:00:00\"}]}";
//Gson gson = new GsonBuilder().registerTypeAdapter(BattleList.class, new BattleListDeserializer()).create();
//Type animalType = new TypeToken<BattleList>() {
//}.getType();
//BattleList bl = gson.fromJson(response, animalType);
//GsonBuilder gsonBuilder = new GsonBuilder();
//gsonBuilder.registerTypeAdapter(BattleList.class, new BattleListDeserializer());
//Gson gson = gsonBuilder.create();
//Gson gson = new Gson();
//JsonParser parser = new JsonParser();
//JsonObject jObject = parser.parse(jsonFormattedString).getAsJsonObject();

//battleList = gson.fromJson(response, BattleList.class);

我的 BattleList 课程:

public class BattleList {

private List<Battles> activeBattles = new ArrayList<>();

public void addBattle(Battles battle){
    activeBattles.add(battle);
}

public Battles getBattle(int indx){
    return activeBattles.get(indx);
}

public int getCount(){
    return activeBattles.size();
}

}

我的战斗课:

public class Battles {

@SerializedName("LiveID")
public int LiveID;

@SerializedName("BattleID")
public int BattleID;

@SerializedName("OvxyzID")
public int OvxyzID;

@SerializedName("ChallengerID")
public int ChallengerID;

@SerializedName("OpponentID")
public int OpponentID;

@SerializedName("ChallengerName")
public String ChallengerName;

@SerializedName("OpponentName")
public String OpponentName;

@SerializedName("TimeChallenged")
public String TimeChallenged;

@SerializedName("IsActive")
public int IsActive;

@SerializedName("TimeEnded")
public String TimeEnded;
}

我不确定这里还包括什么,如果需要更多,我会修改问题。

基本上,我做错了什么?应该很简单,但它让我被屏蔽了一个多星期。

谢谢。

4

0 回答 0