Java - JSONObject 文本必须以 '{' 字符 1 开头

标签 java json jaxb

我一直在尝试解决 JAXB json 问题,但我似乎无法弄清楚出了什么问题。我收到的消息是:

Caused by: org.codehaus.jettison.json.JSONException: A JSONObject text must begin with '{' at character 1 of [{"id":"Daler Kampagne 2","operation":"UPDATE","VoiceIAQStats":{"id":3,"esdId":3,"esdName":"Daler Kampagne 2","nResourcesLoggedIn":0,"nTotalContacts":0,"nHandledContacts":0,"nAbandonedContacts":0,"nDequeuedContacts":0,"avgTalkDuration":0,"longestTalkDuration":0,"avgWaitDuration":0,"longestWaitDuration":0,"longestCurrentlyWaitingDuration":0,"nAvailResources":0,"nInSessionResources":0,"nUnavailResources":0,"nWorkResources":0,"nSelectedResources":0,"nWaitingContacts":0,"nAverageHandlingTimeLowThreshold":0.0,"nAverageHandlingTimeHighThreshold":0.0,"nAverageWaitingTimeLowThreshold":0.0,"nAverageWaitingTimeHighThreshold":0.0,"nAbandonedCallsLowThreshold":0,"nAbandonedCallsHighThreshold":0,"nDequeuedCallsLowThreshold":0,"nDequeuedCallsHighThreshold":0,"nSLAPercentageLowThreshold":0.0,"nSLAPercentageHighThreshold":0.0}},{"id":"Dialer Kampagne 1","operation":"UPDATE","VoiceIAQStats":{"id":2,"esdId":2,"esdName":"Dialer Kampagne 1","nResourcesLoggedIn":0,"nTotalContacts":0,"nHandledContacts":0,"nAbandonedContacts":0,"nDequeuedContacts":0,"avgTalkDuration":0,"longestTalkDuration":0,"avgWaitDuration":0,"longestWaitDuration":0,"longestCurrentlyWaitingDuration":0,"nAvailResources":0,"nInSessionResources":0,"nUnavailResources":0,"nWorkResources":0,"nSelectedResources":0,"nWaitingContacts":0,"nAverageHandlingTimeLowThreshold":0.0,"nAverageHandlingTimeHighThreshold":0.0,"nAverageWaitingTimeLowThreshold":0.0,"nAverageWaitingTimeHighThreshold":0.0,"nAbandonedCallsLowThreshold":0,"nAbandonedCallsHighThreshold":0,"nDequeuedCallsLowThreshold":0,"nDequeuedCallsHighThreshold":0,"nSLAPercentageLowThreshold":0.0,"nSLAPercentageHighThreshold":0.0}},{"id":"ICD01","operation":"UPDATE","VoiceIAQStats":{"id":1,"esdId":1,"esdName":"ICD01","nResourcesLoggedIn":0,"nTotalContacts":0,"nHandledContacts":0,"nAbandonedContacts":0,"nDequeuedContacts":0,"avgTalkDuration":0,"longestTalkDuration":0,"avgWaitDuration":0,"longestWaitDuration":0,"longestCurrentlyWaitingDuration":0,"nAvailResources":0,"nInSessionResources":0,"nUnavailResources":0,"nWorkResources":0,"nSelectedResources":0,"nWaitingContacts":0,"nAverageHandlingTimeLowThreshold":0.0,"nAverageHandlingTimeHighThreshold":0.0,"nAverageWaitingTimeLowThreshold":0.0,"nAverageWaitingTimeHighThreshold":0.0,"nAbandonedCallsLowThreshold":0,"nAbandonedCallsHighThreshold":0,"nDequeuedCallsLowThreshold":0,"nDequeuedCallsHighThreshold":0,"nSLAPercentageLowThreshold":0.0,"nSLAPercentageHighThreshold":0.0}}]
at org.codehaus.jettison.json.JSONTokener.syntaxError(JSONTokener.java:463)
at org.codehaus.jettison.json.JSONObject.<init>(JSONObject.java:204)
at org.codehaus.jettison.mapped.MappedXMLInputFactory.createJSONObject(MappedXMLInputFactory.java:59)
at org.codehaus.jettison.mapped.MappedXMLInputFactory.createXMLStreamReader(MappedXMLInputFactory.java:44)
... 148 more

这是我的类(class):

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class CiscoStatsResponse {

private String id;
private String operation;

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getOperation() {
    return operation;
}

public void setOperation(String operation) {
    this.operation = operation;
}
}

VoiceIAQStats

package dk.netdesign.wallboard.dataprovider.uccxrest.domain;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class VoiceIAQStats {

public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public Integer getEsdId() {
    return esdId;
}
public void setEsdId(Integer esdId) {
    this.esdId = esdId;
}
public String getEsdName() {
    return esdName;
}
public void setEsdName(String esdName) {
    this.esdName = esdName;
}
public Integer getnResourcesLoggedIn() {
    return nResourcesLoggedIn;
}
public void setnResourcesLoggedIn(Integer nResourcesLoggedIn) {
    this.nResourcesLoggedIn = nResourcesLoggedIn;
}
public Integer getnTotalContacts() {
    return nTotalContacts;
}
public void setnTotalContacts(Integer nTotalContacts) {
    this.nTotalContacts = nTotalContacts;
}
public Integer getnHandledContacts() {
    return nHandledContacts;
}
public void setnHandledContacts(Integer nHandledContacts) {
    this.nHandledContacts = nHandledContacts;
}
public Integer getnAbandonedContacts() {
    return nAbandonedContacts;
}
public void setnAbandonedContacts(Integer nAbandonedContacts) {
    this.nAbandonedContacts = nAbandonedContacts;
}
public Integer getnDequeuedContacts() {
    return nDequeuedContacts;
}
public void setnDequeuedContacts(Integer nDequeuedContacts) {
    this.nDequeuedContacts = nDequeuedContacts;
}
public Integer getAvgTalkDuration() {
    return avgTalkDuration;
}
public void setAvgTalkDuration(Integer avgTalkDuration) {
    this.avgTalkDuration = avgTalkDuration;
}
public Integer getLongestTalkDuration() {
    return longestTalkDuration;
}
public void setLongestTalkDuration(Integer longestTalkDuration) {
    this.longestTalkDuration = longestTalkDuration;
}
public Integer getAvgWaitDuration() {
    return avgWaitDuration;
}
public void setAvgWaitDuration(Integer avgWaitDuration) {
    this.avgWaitDuration = avgWaitDuration;
}
public Integer getLongestWaitDuration() {
    return longestWaitDuration;
}
public void setLongestWaitDuration(Integer longestWaitDuration) {
    this.longestWaitDuration = longestWaitDuration;
}
public Integer getLongestCurrentlyWaitingDuration() {
    return longestCurrentlyWaitingDuration;
}
public void setLongestCurrentlyWaitingDuration(
        Integer longestCurrentlyWaitingDuration) {
    this.longestCurrentlyWaitingDuration = longestCurrentlyWaitingDuration;
}
public Integer getnAvailResources() {
    return nAvailResources;
}
public void setnAvailResources(Integer nAvailResources) {
    this.nAvailResources = nAvailResources;
}
public Integer getnInSessionResources() {
    return nInSessionResources;
}
public void setnInSessionResources(Integer nInSessionResources) {
    this.nInSessionResources = nInSessionResources;
}
public Integer getnUnavailResources() {
    return nUnavailResources;
}
public void setnUnavailResources(Integer nUnavailResources) {
    this.nUnavailResources = nUnavailResources;
}
public Integer getnWorkResources() {
    return nWorkResources;
}
public void setnWorkResources(Integer nWorkResources) {
    this.nWorkResources = nWorkResources;
}
public Integer getnSelectedResources() {
    return nSelectedResources;
}
public void setnSelectedResources(Integer nSelectedResources) {
    this.nSelectedResources = nSelectedResources;
}
public Integer getnWaitingContacts() {
    return nWaitingContacts;
}
public void setnWaitingContacts(Integer nWaitingContacts) {
    this.nWaitingContacts = nWaitingContacts;
}
public Integer getnAverageHandlingTimeLowThreshold() {
    return nAverageHandlingTimeLowThreshold;
}
public void setnAverageHandlingTimeLowThreshold(
        Integer nAverageHandlingTimeLowThreshold) {
    this.nAverageHandlingTimeLowThreshold = nAverageHandlingTimeLowThreshold;
}
public Integer getnAverageHandlingTimeHighThreshold() {
    return nAverageHandlingTimeHighThreshold;
}
public void setnAverageHandlingTimeHighThreshold(
        Integer nAverageHandlingTimeHighThreshold) {
    this.nAverageHandlingTimeHighThreshold = nAverageHandlingTimeHighThreshold;
}
public Integer getnAverageWaitingTimeLowThreshold() {
    return nAverageWaitingTimeLowThreshold;
}
public void setnAverageWaitingTimeLowThreshold(
        Integer nAverageWaitingTimeLowThreshold) {
    this.nAverageWaitingTimeLowThreshold = nAverageWaitingTimeLowThreshold;
}
public Integer getnAverageWaitingTimeHighThreshold() {
    return nAverageWaitingTimeHighThreshold;
}
public void setnAverageWaitingTimeHighThreshold(
        Integer nAverageWaitingTimeHighThreshold) {
    this.nAverageWaitingTimeHighThreshold = nAverageWaitingTimeHighThreshold;
}
public Integer getnAbandonedCallsLowThreshold() {
    return nAbandonedCallsLowThreshold;
}
public void setnAbandonedCallsLowThreshold(Integer nAbandonedCallsLowThreshold) {
    this.nAbandonedCallsLowThreshold = nAbandonedCallsLowThreshold;
}
public Integer getnAbandonedCallsHighThreshold() {
    return nAbandonedCallsHighThreshold;
}
public void setnAbandonedCallsHighThreshold(Integer nAbandonedCallsHighThreshold) {
    this.nAbandonedCallsHighThreshold = nAbandonedCallsHighThreshold;
}
public Integer getnDequeuedCallsLowThreshold() {
    return nDequeuedCallsLowThreshold;
}
public void setnDequeuedCallsLowThreshold(Integer nDequeuedCallsLowThreshold) {
    this.nDequeuedCallsLowThreshold = nDequeuedCallsLowThreshold;
}
public Integer getnDequeuedCallsHighThreshold() {
    return nDequeuedCallsHighThreshold;
}
public void setnDequeuedCallsHighThreshold(Integer nDequeuedCallsHighThreshold) {
    this.nDequeuedCallsHighThreshold = nDequeuedCallsHighThreshold;
}
public Integer getnSLAPercentageLowThreshold() {
    return nSLAPercentageLowThreshold;
}
public void setnSLAPercentageLowThreshold(Integer nSLAPercentageLowThreshold) {
    this.nSLAPercentageLowThreshold = nSLAPercentageLowThreshold;
}
public Integer getnSLAPercentageHighThreshold() {
    return nSLAPercentageHighThreshold;
}
public void setnSLAPercentageHighThreshold(Integer nSLAPercentageHighThreshold) {
    this.nSLAPercentageHighThreshold = nSLAPercentageHighThreshold;
}
String id;
Integer esdId;
String esdName;
Integer nResourcesLoggedIn;
Integer nTotalContacts;
Integer nHandledContacts;
Integer nAbandonedContacts;
Integer nDequeuedContacts;
Integer avgTalkDuration;
Integer longestTalkDuration;
Integer avgWaitDuration;
Integer longestWaitDuration;
Integer longestCurrentlyWaitingDuration;
Integer nAvailResources;
Integer nInSessionResources;
Integer nUnavailResources;
Integer nWorkResources;
Integer nSelectedResources;
Integer nWaitingContacts;
Integer nAverageHandlingTimeLowThreshold;
Integer nAverageHandlingTimeHighThreshold;
Integer nAverageWaitingTimeLowThreshold;
Integer nAverageWaitingTimeHighThreshold;
Integer nAbandonedCallsLowThreshold;
Integer nAbandonedCallsHighThreshold;
Integer nDequeuedCallsLowThreshold;
Integer nDequeuedCallsHighThreshold;
Integer nSLAPercentageLowThreshold;
Integer nSLAPercentageHighThreshold;
}

VoiceIAQStatsResponse:

package dk.netdesign.wallboard.dataprovider.uccxrest.domain;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class VoiceIAQStatsResponse extends CiscoStatsResponse {

@XmlElement(name = "VoiceIAQStats")
private VoiceIAQStats voiceIAQStats;

public VoiceIAQStats getVoiceIAQStats() {
    return voiceIAQStats;
}

public void setVoiceIAQStats(VoiceIAQStats voiceIAQStats) {
    this.voiceIAQStats = voiceIAQStats;
}

}

json 看起来像这样(这对我来说似乎是正确的?)

[{
"id": "Daler Kampagne 2",
"operation": "UPDATE",
"VoiceIAQStats": {
    "id": 3,
    "esdId": 3,
    "esdName": "Daler Kampagne 2",
    "nResourcesLoggedIn": 0,
    "nTotalContacts": 0,
    "nHandledContacts": 0,
    "nAbandonedContacts": 0,
    "nDequeuedContacts": 0,
    "avgTalkDuration": 0,
    "longestTalkDuration": 0,
    "avgWaitDuration": 0,
    "longestWaitDuration": 0,
    "longestCurrentlyWaitingDuration": 0,
    "nAvailResources": 0,
    "nInSessionResources": 0,
    "nUnavailResources": 0,
    "nWorkResources": 0,
    "nSelectedResources": 0,
    "nWaitingContacts": 0,
    "nAverageHandlingTimeLowThreshold": 0.0,
    "nAverageHandlingTimeHighThreshold": 0.0,
    "nAverageWaitingTimeLowThreshold": 0.0,
    "nAverageWaitingTimeHighThreshold": 0.0,
    "nAbandonedCallsLowThreshold": 0,
    "nAbandonedCallsHighThreshold": 0,
    "nDequeuedCallsLowThreshold": 0,
    "nDequeuedCallsHighThreshold": 0,
    "nSLAPercentageLowThreshold": 0.0,
    "nSLAPercentageHighThreshold": 0.0
   }
     }, {
"id": "Dialer Kampagne 1",
"operation": "UPDATE",
"VoiceIAQStats": {
    "id": 2,
    "esdId": 2,
    "esdName": "Dialer Kampagne 1",
    "nResourcesLoggedIn": 0,
    "nTotalContacts": 0,
    "nHandledContacts": 0,
    "nAbandonedContacts": 0,
    "nDequeuedContacts": 0,
    "avgTalkDuration": 0,
    "longestTalkDuration": 0,
    "avgWaitDuration": 0,
    "longestWaitDuration": 0,
    "longestCurrentlyWaitingDuration": 0,
    "nAvailResources": 0,
    "nInSessionResources": 0,
    "nUnavailResources": 0,
    "nWorkResources": 0,
    "nSelectedResources": 0,
    "nWaitingContacts": 0,
    "nAverageHandlingTimeLowThreshold": 0.0,
    "nAverageHandlingTimeHighThreshold": 0.0,
    "nAverageWaitingTimeLowThreshold": 0.0,
    "nAverageWaitingTimeHighThreshold": 0.0,
    "nAbandonedCallsLowThreshold": 0,
    "nAbandonedCallsHighThreshold": 0,
    "nDequeuedCallsLowThreshold": 0,
    "nDequeuedCallsHighThreshold": 0,
    "nSLAPercentageLowThreshold": 0.0,
    "nSLAPercentageHighThreshold": 0.0
 }
  }, {
"id": "ICD01",
"operation": "UPDATE",
"VoiceIAQStats": {
    "id": 1,
    "esdId": 1,
    "esdName": "ICD01",
    "nResourcesLoggedIn": 0,
    "nTotalContacts": 0,
    "nHandledContacts": 0,
    "nAbandonedContacts": 0,
    "nDequeuedContacts": 0,
    "avgTalkDuration": 0,
    "longestTalkDuration": 0,
    "avgWaitDuration": 0,
    "longestWaitDuration": 0,
    "longestCurrentlyWaitingDuration": 0,
    "nAvailResources": 0,
    "nInSessionResources": 0,
    "nUnavailResources": 0,
    "nWorkResources": 0,
    "nSelectedResources": 0,
    "nWaitingContacts": 0,
    "nAverageHandlingTimeLowThreshold": 0.0,
    "nAverageHandlingTimeHighThreshold": 0.0,
    "nAverageWaitingTimeLowThreshold": 0.0,
    "nAverageWaitingTimeHighThreshold": 0.0,
    "nAbandonedCallsLowThreshold": 0,
    "nAbandonedCallsHighThreshold": 0,
    "nDequeuedCallsLowThreshold": 0,
    "nDequeuedCallsHighThreshold": 0,
    "nSLAPercentageLowThreshold": 0.0,
    "nSLAPercentageHighThreshold": 0.0
}
}]

编辑:这是我用来调用网络服务并返回 VoiceIAQStatsResponse 列表的方法。

public List<VoiceIAQStatsResponse> getVoiceIAQStatsResponse() {

    WebTarget resourceWebTarget = getBaseTarget().path("VoiceIAQStats");

    Invocation.Builder invocationBuilder = resourceWebTarget
            .request(MediaType.APPLICATION_JSON);

    javax.ws.rs.core.Response response = invocationBuilder.get();

    System.out.println("ÅH ABE" + response.toString());

    GenericType<List<VoiceIAQStatsResponse>> type = new GenericType<List<VoiceIAQStatsResponse>>() {
    };
    logger.debug(response.getStatusInfo().toString());

    List<VoiceIAQStatsResponse> list = null;
    if (response.getStatusInfo().getFamily().compareTo(Response.Status.Family.SUCCESSFUL) == 0) {

        list = response.readEntity(type);
    } else {
        list = Collections.EMPTY_LIST;
    }

    return list;
}

我真的希望有人有解决这个问题的经验 - 我似乎无法弄清楚这个问题。

最佳答案

在这种情况下,错误消息非常准确。您拥有的是 JSON 数组,而不是 JSON 对象。不过,您始终可以轻松地转换它们:

{"operations": [{
"id": "Daler Kampagne 2",
"operation": "UPDATE",
 ...
}]}

是一个有效的 JSON。

然后您将需要另一个顶级元素:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class VoiceIAQStatsResponses {

@XmlElement(name = "operations")
private List<VoiceIAQStatsResponse> operations;

...

}

关于Java - JSONObject 文本必须以 '{' 字符 1 开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40804774/

相关文章:

java - 带有嵌套谷歌地图 fragment 的 fragment 中后按 NullPointerException

ios - 将 TableView Controller 更改为 Collection View Controller

javascript - 从 Express 获取 Google map 地理编码 JSON

java - JSONObject 类型的方法 getJSONObject(String) 未定义

java - 在 wsimport 中使用 Maven 坐标样式情节

android - 在 Android 上使用 webdav

java - jaxb可以生成这样的xml模式吗?

java - 找不到访问器/修改器方法的变量

java - Selenium 测试 - 无法从日历列表中选择值

java - 子对象具有父对象作为属性 JPA 导致无尽的 JSON