Question

I am new to JSON web service. I want to convert below simple JSON structure to Java String. Though I have referred many sites, still it adds more confusion to me. I am using GSON for parsing but alwasy getting

"java.lang.IllegalStateException: This is not a JSON Array."

Please assist me to resolve the issue.

JSON DATA : {"data1":"100","data2":"hello"}

JAVA CODE :

private void getPostMessage(String msg) {
        try {       
                EmployeeBean emp;
                String json;

            Client client = Client.create();
            WebResource webResource = client.resource("http://localhost:8080/JSON_EMP_Serv/rest/server/post/");

            ClientResponse response = webResource.type("application/json").post(ClientResponse.class,msg);
            if (response.getStatus() != 201) {
                throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
            }

            String output = response.getEntity(String.class);
            System.out.println("\n============get POST Message Response============");
            System.out.println(output);

            /******* JSON PARSER **********/


           Gson gson = new Gson();
           JsonParser parser = new JsonParser();
           JsonArray Jarray = parser.parse(output).getAsJsonArray();

           ArrayList<EmployeeBean> lcs = new ArrayList<EmployeeBean>();

         for(JsonElement obj : Jarray )
            {
                emp = gson.fromJson(obj,EmployeeBean.class);

                lcs.add(emp);

            }
            int length=lcs.size();
            System.out.println("ARRAY LENGTH"+length);
            for(int i=0;i<length;i++)
            {
                System.out.println(lcs.get(i)+"\n");
            }

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

EMPLOYEEBEAN CLASS :

package com.pats.client.bean;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.annotations.SerializedName;

    public class EmployeeBean {


    private String data1= null;
    private String data2 = null;

    public EmployeeBean(String data1,String data2)
    {

        this.data1=data1;
        this.data2=data2;
    }

    public String getData1()
    { return data1; }
    public String getData2()
    { return data2; }
    public void setData1(String data1)
    {
        this.data1=data1;
    }
    public void setData2(String data2)
    {
        this.data2=data2;
    }
    @Override
    public String toString() {
         //return "[data1=" + data1 + ", data2=" + data2 + "]";

        return " DATA-1 : " + this.data1 + "DATA-2 : " + this.data2;
    }

}
Was it helpful?

Solution

The point is, clearly, that your data is not a JSON array.

{"data1":"100","data2":"hello"}

It is, instead, a JSON object, but you are trying to parse it and get a JsonArray. You could change this and use getAsJsonObject() instead, but from your code I think it's your starting data that is wrong. I guess you should have an array of employees, so the correct data should probably be something like:

[{"data1":"100","data2":"hello"}]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top