簡體   English   中英

如何從Java中包含諸如':','['和']'等字符的String創建Json對象

[英]How to create Json object from String containing characters like ':' ,'[' and ']' in Java

我有一個要從中創建一個jsonobject的字符串

import org.json.JSONObject;

JSONObject json=new JSONObject("{success=false, errorMessage=Application with appId : [randomAppId] not registered, errorCode=102}");
System.out.println("JSON:"+json.toString());

但是由於字符串中的:[]類的字符,我現在遇到了異常。

堆棧跟蹤:

Exception in thread "main" org.json.JSONException: Expected a ',' or '}' at character 53
    at org.json.JSONTokener.syntaxError(JSONTokener.java:410)
    at org.json.JSONObject.<init>(JSONObject.java:222)
    at org.json.JSONObject.<init>(JSONObject.java:402)

問題是,您的字符串不是有效的JSON。 嘗試:

JSONObject json=new JSONObject("{\"success\":false, \"errorMessage\":\"Application with appId : [randomAppId] not registered\", \"errorCode\":102}");

暫無
暫無

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

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