简体   繁体   English

org.json.JSONException:JSONArray [0]不是JSONObject

[英]org.json.JSONException: JSONArray[0] is not a JSONObject

Output of String jsreturn: [{"type":1, "msg":"ERROR"}] . String jsreturn: [{"type":1, "msg":"ERROR"}]输出String jsreturn: [{"type":1, "msg":"ERROR"}]

I'm trying to get the content of msg key which is ERROR. 我正在尝试获取msg键的内容,这是错误的。 I'm doing that by transforming string return into an array. 我这样做是通过将字符串return转换为数组。 However I'm getting some errors commented next to each line on the follow code. 但是我在以下代码的每一行旁边都注释了一些错误。 Any insight? 有见识吗?

 Vector<ClsReturn> ret = null;
 ret = ds.id(collection, "fs",in_uri );

 String jsReturn = ret.toString();

 JSONObject myJsonObject = new JSONObject(ret);

 JSONArray array = new JSONArray(jsReturn); 

 int i = 0;

 while(i < array.length()){
     myJsonObject = array.getJSONObject(i); //org.json.JSONException: JSONArray[0] is not a JSONObject.
     System.out.println(myJsonObject.getString("msg"));
     i++;
 }

I dont know if I have understood your question, I think it is easy, this works: 我不知道我是否理解您的问题,我认为这很容易,这可行:

    String jsReturn =" [{\"type\":1, \"txt\":\"ERROR\"}]";
    JSONArray array = new JSONArray(jsReturn); 
    int i = 0;
    JSONObject myJsonObject = new JSONObject();
    while(i < array.length()){
        myJsonObject = array.getJSONObject(i); 
        System.out.println(myJsonObject.getString("txt"));
        i++;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM