0

I want to convert my json string to string array. My JSON string is having two parameters "href" and "name". I want to create List of string of values of "name" parameter using java. I am using NetBeans for my application. please help me out to resolve this issue. I am getting error as

Exception in thread "AWT-EventQueue-0" org.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1]

JSONArray arr = new JSONArray(response);
List<String> list = new ArrayList<String>();
for(int i = 0; i < arr.length(); i++){
    list.add(arr.getJSONObject(i).getString("name"));
    System.out.println(arr.getJSONObject(i).getString("name"));
}

This is my JSON string

[
    {
        "href": "\/api\/rest\/v1\/protocols\/bacnet\/local\/objects\/analog-value\/1",
        "name": "analogValue_1"
    },
    {
        "href": "\/api\/rest\/v1\/protocols\/bacnet\/local\/objects\/analog-value\/9",
        "name": "analogValue_9"
    },
    {
        "href": "\/api\/rest\/v1\/protocols\/bacnet\/local\/objects\/analog-value\/2",
        "name": "analogValue_2"
    },
    {
        "href": "\/api\/rest\/v1\/protocols\/bacnet\/local\/objects\/analog-value\/8",
        "name": "analogValue_8"
    },
    {
        "href": "\/api\/rest\/v1\/protocols\/bacnet\/local\/objects\/analog-value\/7",
        "name": "analogValue_7"
    },
    {
        "href": "\/api\/rest\/v1\/protocols\/bacnet\/local\/objects\/analog-value\/3",
        "name": "analogValue_3"
    },
    {
        "href": "\/api\/rest\/v1\/protocols\/bacnet\/local\/objects\/analog-value\/6",
        "name": "analogValue_6"
    },
    {
        "href": "\/api\/rest\/v1\/protocols\/bacnet\/local\/objects\/analog-value\/5",
        "name": "analogValue_5"
    },
    {
        "href": "\/api\/rest\/v1\/protocols\/bacnet\/local\/objects\/analog-value\/4",
        "name": "analogValue_4"
    }
]
gprathour
  • 14,813
  • 5
  • 66
  • 90
PDTech
  • 149
  • 2
  • 5
  • 12
  • What does response contain? As the error message tells us it's not starting with "[" – eol Jun 19 '17 at 10:51
  • For some reason your string is not a valid JSON string, so the json you show is not the input json – Denny Jun 19 '17 at 10:51
  • Json stringI have posted is the response I am getting from my rest api query – PDTech Jun 19 '17 at 10:57
  • I checked your example and my run works fine.So the problem is that your response a bit another than this one – ema Jun 19 '17 at 10:58
  • System.out.println(response.body().toString()); getting okhttp3.internal.http.RealResponseBody@66cdc1bd not the actual response why so ? – PDTech Jun 19 '17 at 11:07

2 Answers2

0

Fix your json. 1. Change square brackets to curly brackets. 2. Each dictionary inside of your json is a value which must have a corresponding key. You code should look like this:

public static void main(String[] args) {

        String myJSON = "{data_0:\n"
                + "    {\n"
                + "        \"href\": \"\\/api\\/rest\\/v1\\/protocols\\/bacnet\\/local\\/objects\\/analog-value\\/1\",\n"
                + "        \"name\": \"analogValue_1\"\n"
                + "    },\n data_1:"
                + "    {\n"
                + "        \"href\": \"\\/api\\/rest\\/v1\\/protocols\\/bacnet\\/local\\/objects\\/analog-value\\/9\",\n"
                + "        \"name\": \"analogValue_9\"\n"
                + "    },\n data_2:"
                + "    {\n"
                + "        \"href\": \"\\/api\\/rest\\/v1\\/protocols\\/bacnet\\/local\\/objects\\/analog-value\\/2\",\n"
                + "        \"name\": \"analogValue_2\"\n"
                + "    },\n data_3:"
                + "    {\n"
                + "        \"href\": \"\\/api\\/rest\\/v1\\/protocols\\/bacnet\\/local\\/objects\\/analog-value\\/8\",\n"
                + "        \"name\": \"analogValue_8\"\n"
                + "    },\n data_4:"
                + "    {\n"
                + "        \"href\": \"\\/api\\/rest\\/v1\\/protocols\\/bacnet\\/local\\/objects\\/analog-value\\/7\",\n"
                + "        \"name\": \"analogValue_7\"\n"
                + "    },\n data_5:"
                + "    {\n"
                + "        \"href\": \"\\/api\\/rest\\/v1\\/protocols\\/bacnet\\/local\\/objects\\/analog-value\\/3\",\n"
                + "        \"name\": \"analogValue_3\"\n"
                + "    },\n data_6:"
                + "    {\n"
                + "        \"href\": \"\\/api\\/rest\\/v1\\/protocols\\/bacnet\\/local\\/objects\\/analog-value\\/6\",\n"
                + "        \"name\": \"analogValue_6\"\n"
                + "    },\n data_7:"
                + "    {\n"
                + "        \"href\": \"\\/api\\/rest\\/v1\\/protocols\\/bacnet\\/local\\/objects\\/analog-value\\/5\",\n"
                + "        \"name\": \"analogValue_5\"\n"
                + "    },\n data_8:"
                + "    {\n"
                + "        \"href\": \"\\/api\\/rest\\/v1\\/protocols\\/bacnet\\/local\\/objects\\/analog-value\\/4\",\n"
                + "        \"name\": \"analogValue_4\"\n"
                + "    }\n"
                + "}";

        JSONObject jsonObject = new JSONObject(myJSON);
        System.out.println("jsonObject: " + jsonObject.toString());

        List<String> list = new ArrayList<String>();
        System.out.println("jsonObject length: " + jsonObject.length());
        for (int i = 0; i < jsonObject.length(); i++) {
            list.add(jsonObject.getJSONObject("data_" + i).toString());
            System.out.println(jsonObject.getJSONObject("data_" + i));
        }
    }

I added keys from data_0 to data_8. Then you create a list. Probably that does not exactly solve your problem, but least gives an idea where you're making a mistake.

Nurjan
  • 5,889
  • 5
  • 34
  • 54
0

From your comment we can see that you're using okhttp3.internal.http.RealResponseBody. Since the toString() method is not overwritten, the default implementation is used which is why System.out.println(response.body().toString()); prints okhttp3.internal.http.RealResponseBody@66cdc1bd.

To get the actual raw response use the string() method:

JSONArray arr = new JSONArray(responseBody.string());

According to the documentation (RealResponseBody extends ResponseBody):

String  string()
Returns the response as a string decoded with the charset of the Content-Type header.

This also has already been discussed here by the way.

eol
  • 23,236
  • 5
  • 46
  • 64