【问题标题】:Value https of type java.lang.String cannot be converted to JSONArrayjava.lang.String 类型的值 https 无法转换为 JSONArray
【发布时间】:2016-05-29 07:53:38
【问题描述】:

我正在尝试从 json 数组中获取 Json 字符串,但出现此错误,任何帮助将不胜感激,下面给出 Json 格式,请帮助某人。 错误如下

05-29 12:37:22.600 25505-25505/com.akasha.mongodataapi W/System.err: org.json.JSONException: Value https of type java.lang.String cannot be converted to JSONArray
05-29 12:37:22.610 25505-25505/com.akasha.mongodataapi W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)
05-29 12:37:22.610 25505-25505/com.akasha.mongodataapi W/System.err:     at org.json.JSONArray.<init>(JSONArray.java:96)
05-29 12:37:22.610 25505-25505/com.akasha.mongodataapi W/System.err:     at org.json.JSONArray.<init>(JSONArray.java:108)

它是 Json 格式

 [ { "_id" : { "$oid" : "57472009a0fdab7cc3c"} , "name" : "Sasha Burni" , "sort" : "Sasha"} ,
 { "_id" : { "$oid" : "57472009afdab7cc3d"} , "name" : "Akasha Khail" , "sort" : "Akasha"}]

Java 代码如下

 String url="https://api.mlab.com/api/1/databases/picasanovels/collections/Country?apiKey=myapikey";

    try {
                JSONArray jArr = new JSONArray(url);
                for (int count = 0; count < jArr.length(); count++) {
                    JSONObject obj = jArr.getJSONObject(count);
                    String name = obj.getString("name");
                    System.out.println("Name Printed :"+name);
                    //so on
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

【问题讨论】:

  • 看起来它正在尝试将您的 url 转换为 JSON 数组。您必须发出 GET 请求以获取该 URL 的内容,然后将内容作为参数传递给 JSONArray 对象
  • 你不是将字符串的 url 转换为 json 数组吗?我猜你想转换你没有做出的 http 请求的响应。使用 Unirest 或 apache http 客户端等 http 客户端发出请求。
  • 感谢您的早期响应,但如果有一些例子会很容易理解。

标签: java json mongodb


【解决方案1】:
  1. 您正在尝试将 API 调用(url 地址)传递给 JSON ARRAY。它应该是 API 调用的响应。
  2. 您必须创建一个 URL 对象并将 URL 传递给构造函数。
  3. 然后您必须使用 HttpUrlConnection 类打开一个新连接。

我在下面包含了完整的代码供您参考。如果您需要更多帮助,请随时询问。谢谢

package com.pragin.ocjp;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class JsonExample {

public String jsonEx(){
    String url = "  https://api.tfl.gov.uk/Place?lat=51.555504&lon=0.0592359&radius=80&includeChildren=False&type=NaptanPublicBusCoachTram&app_id=95a7b158&app_key=a3acd48055f470cf35ab5f6f360604c5";
    URL obj;
    BufferedReader reader;
    StringBuilder stringBuilder;
    HttpURLConnection con;
    stringBuilder = new StringBuilder();
    try {
        obj = new URL(url);
         con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("GET");
        int responseCode = con.getResponseCode(); // to check success and failure of API call

        System.out.println("Response Code : " + responseCode);
        String response = con.getResponseMessage();

        System.out.println("Response : " + response.toString());

        reader = new BufferedReader(new InputStreamReader(con.getInputStream()));


        String line = null;

        while ((line = reader.readLine()) != null){
            stringBuilder.append(line + "\n");

            System.out.println("String : " + stringBuilder.toString());
        }
        //return stringBuilder.toString();

    }catch(IOException e){
        System.out.println("Error" + e);
    }
    return stringBuilder.toString();

}

public void jsonParser(String res) throws ParseException {
    JSONParser jParse = new JSONParser();

    JSONObject jsonObject = (JSONObject) jParse.parse(res);
    System.out.println("res = [" + jsonObject.size() + "]");
    for(int i = 0; i <jsonObject.size(); i++ ){
       // JSONObject jsonObject = (JSONObject) jArray.get(i);
       // jsonObject.
        String name = (String) jsonObject.get("$type");
        System.out.println("Name : " + name);
    }

}

public static void main(String[] args) throws ParseException {
    JsonExample js = new JsonExample();
    js.jsonParser(js.jsonEx());
}

}`

【讨论】:

    猜你喜欢
    • 2016-02-21
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多