JSONArray初始值应该是字符串或集合或数组

时间:2016-09-12 05:50:36

标签: java json api curl httprequest

我是初学JAVA的API。

我在节目中得到的回应如下:

*对象响应:

    {
        "statusLine":{
            "reasonPhrase":"OK",
            "protocolVersion":{},
            "statusCode":200
        },
        "protocolVersion":{},
        "locale":"en_IN",
        "entity":{
            "repeatable":false,
            "content":{}
        }
    } 

异常发生。 JSONArray初始值应该是字符串或集合或数组。*

无法在结果中获得完整的完整值。我只收到标题详细信息。

请帮助我从API获取完整的详细信息。使用JAVA

package IOT;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.json.JSONObject;
import org.json.JSONArray;

public class HttpPostWithBody {

public static void main(String args[]) {
    String Message = "6f2159f998";

    try {
        new HttpPostWithBody().sendJSONData(Message);
    } catch (Exception E) {
        System.out.println("Exception Occured. " + E.getMessage());
    }
}

public String sendJSONData(String message) throws Exception {

    Map< String, Object> jsonValues = new HashMap< String, Object>();
    {
    jsonValues.put("thing_key", message);
    jsonValues.put("from", "2016/08/29 16:55:00");
    jsonValues.put("to", "2016/08/29 17:05:00");
    jsonValues.put("time_zone", "Mumbai");
    jsonValues.put("time_format", "str");
    jsonValues.put("per", 50);
    //jsonValues.put("metrics", "1st data");
    }
    JSONObject json = new JSONObject(jsonValues);

    String url = "https://api.datonis.io/api/v3/datonis_query/thing_data";

    DefaultHttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);
    post.setHeader("Content-Type", "application/json");
    post.setHeader("Accept", "application/json");
    post.setHeader("X-Auth-Token", "YZ0Sa7IfpdtAXQQ-2CNeAg");

    StringEntity entity = new StringEntity(json.toString(), "UTF8");
    System.out.println("This is the entity: " + entity);

    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    JsonParser jp = new JsonParser();
    JsonElement je = jp.parse(json.toString());
    String prettyJsonString = gson.toJson(je);
    System.out.println("Pretty Json: " + prettyJsonString);

    entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));

    post.setEntity(entity);
    //this is your response:

    HttpResponse response = client.execute(post);

    JSONObject jo = new JSONObject(response);   
    System.out.println("Object Response: " + jo);

    JSONArray ab = new JSONArray(response);
    JSONObject cd = ab.getJSONObject(0); 

    System.out.println("Array Response: " + cd);
    System.out.println("Array Response: " + jo.getJSONArray("6f2159f998"));
    System.out.println("Connection Status: " + response.getStatusLine());
    return response.getStatusLine().toString();

    }
}

1 个答案:

答案 0 :(得分:0)

首先,您必须确认响应数据是有效的JSON数据。使用一些JSON检查站点,您可以轻松找到您的json有效。格式化后: { "statusLine": { "reasonPhrase": "OK", "protocolVersion": {}, "statusCode": 200 }, "protocolVersion": {}, "locale": "en_IN", "entity": { "repeatable": false, "content": {} } } 现在,问题是此JSON数据是JSON Object,而不是JSON Array。因此,句子JSONArray ab = new JSONArray(response);将导致错误。如果您想获得statusLine结果,可以使用以下代码: JSONObject jo = new JSONObject(response); String statusLine = jo.getJSONObject("statusLine").toString();