【问题标题】:parsing URL in String from JSON从 JSON 中解析字符串中的 URL
【发布时间】:2013-04-01 00:32:23
【问题描述】:

1) 我有一个 JSON 文件:

{
  "serverURI":"http://localhost:8080/PocketUNI_API/serverURLs",
  "newsURI":"http://localhost:8080/gateway/rss/rss.xml",
  "modulesURI":"http://localhost:8080/PocketUNI_API/modules"
}

2) 我需要在 Java 客户端上以字符串格式获取 URL。

String json = jsonReceiver.makeHttpRequest(URL_SERVER, "GET", params);
JSONArray uris = new JSONArray(json);

Receiver 工作正常,json 显示接收到的正确字符串,但是当它使用 JSONArray 解析时会抛出错误

org.json.JSONException: Value {"serverURI":"http:\/\/192.168.0.... of type org.json.JSONObject cannot be converted to JSONArray. 

问题:如何解析带有 URL 值的 json?

【问题讨论】:

  • 您为什么要尝试将 Object 转换为 Array ?
  • 使用 jsonArray 对象循环遍历多个 json 对象
  • 你在 dosnt 上面发布的代码似乎有一个 jsonobject 数组,而不是它有一个 json 对象,并且将 obejct 转换为数组会给你 jsonexception 尝试提取单个对象
  • 不是JSONArray,而是JSONObject。 JSONObject json = new JSONObject(str)

标签: java json arrays jsonexception


【解决方案1】:

你得到的不是JSONArray,而是JSONObject

JSONObject uris = new JSONObject(json);

【讨论】:

    【解决方案2】:

    json 是 json 对象而不是数组,这就是您收到错误的原因。数组将被包裹在[] 中,对象被包裹在{} 中。

    JSONObject uris = new JSONObject (json);
    

    【讨论】:

    • 非常感谢您的帮助!:) 对不起,不能竖起大拇指,没有足够的声誉。
    【解决方案3】:

    您必须使用JSONObject,而不是JSONArray

    JSONObject uris = new JSONObject(json);
    

    【讨论】:

      【解决方案4】:

      在您的代码中,只需将 JSONArray 替换为 JSONobject

      JSONObject uris = new JSONObject(json);
      

      【讨论】:

      • 非常感谢您的帮助!:) 对不起,不能竖起大拇指,没有足够的声誉。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-13
      • 2013-06-10
      • 2021-10-29
      • 1970-01-01
      相关资源
      最近更新 更多