簡體   English   中英

創建JSON對象時出錯

[英]Error in Creating JSON object

我正在獲取一個JSON數組作為字符串值,我需要使用該數組創建一個JSON對象。 數組代碼是這樣的。

{"eventsList" : [
    "requestId" : "82334-adf86d-8bac8ef-289c"
    events:[
        {
            "eventType" : "receiveLocation_Event", 
            "externalId" : "973af2f8-820b-457b-89c2", 
            "description" : "Test Event",
            "whenOccurred" : "06-Aug-2013 07.15.01.0 AM",
            "partnerId" : "cecdbd94-ac60-4db0-b7f2", 
            "tagsAndValues" : { 
                 "locationAccuracy" : "10",
                 "attr2" : "value2"
            },
            "count" : "2" 
         },
         {
            "eventType" : "SEND_SMS_sendSmsEvent",
            "externalId" : "45af4f8-87-4f42b-832abc",
            "description" : "Another Test Event",
            "whenOccurred" : "06-Aug-2013 08.16.01.0 AM",
            "partnerId" : "cecdbd94-ac60-4db0-b7f2",
            "tagsAndValues" : { 
                  "messageLength" : "135",
                  "attrX" : "valueX"
             }, 
            "count" : "1" 
          }
    ]

 }

]
}

我嘗試使用以下代碼行創建JSON對象

SONObject jsonObject = new JSONObject(string);

運行此程序時出現錯誤。

org.json.JSONException: Expected a ',' or ']' at character 35
    at org.json.JSONTokener.syntaxError(JSONTokener.java:413)
    at org.json.JSONArray.<init>(JSONArray.java:143)
    at org.json.JSONTokener.nextValue(JSONTokener.java:351)
    at org.json.JSONObject.<init>(JSONObject.java:206)
    at org.json.JSONObject.<init>(JSONObject.java:420)

請幫我解決這個問題。

有幾個錯誤。

  • [后面是逗號分隔值的列表,但是在"requestId"之后有一個冒號。 您可能希望第1行上的[{

  • 考慮到最后一個問題,您可能需要在"82334-adf86d-8bac8ef-289c"之后"82334-adf86d-8bac8ef-289c"逗號

如果將文本放到在線JSON格式器和驗證器中(例如此文本) ,它將指出所有錯誤。

問題在這里:...“ requestId”:“ 82334-adf86d-8bac8ef-289c”事件:...

您忘記了一些標點符號:...“ requestId”:“ 82334-adf86d-8bac8ef-289c”,“事件”:......

使用它代替這是JSON語法。 所有鍵都是字符串。

這就是String的樣子;

{"eventsList" : [
    {"requestId" : "82334-adf86d-8bac8ef-289c"},
   { "events":[
        {
            "eventType" : "receiveLocation_Event", 
            "externalId" : "973af2f8-820b-457b-89c2", 
            "description" : "Test Event",
            "whenOccurred" : "06-Aug-2013 07.15.01.0 AM",
            "partnerId" : "cecdbd94-ac60-4db0-b7f2", 
            "tagsAndValues" : { 
                 "locationAccuracy" : "10",
                 "attr2" : "value2"
            },
            "count" : "2" 
         },
         {
            "eventType" : "SEND_SMS_sendSmsEvent",
            "externalId" : "45af4f8-87-4f42b-832abc",
            "description" : "Another Test Event",
            "whenOccurred" : "06-Aug-2013 08.16.01.0 AM",
            "partnerId" : "cecdbd94-ac60-4db0-b7f2",
            "tagsAndValues" : { 
                  "messageLength" : "135",
                  "attrX" : "valueX"
             }, 
            "count" : "1" 
          }
    ]

 }
 ]
}

requestId和事件必須是這樣的: {"requestId" : "82334-adf86d-8bac8ef-289c"}, { "events":而且在關閉內部JSONArray之后也必須關閉}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM