2017-07-12 19 views
0

私は、エンドポイントhttp://127.0.0.1:4567/suppliersでGETを実行するときに以下のJSONを提供する安らかなエンドポイントを持っています。私はSupplierResponseTestオブジェクトに上記データを解析するGSONを使用httpPost要求にgsonを使用したjson配列内のオブジェクトの解析

{ 
    "status": "SUCCESS", 
    "jsonapi": { 
     "version": "1.0" 
    }, 
    "data": { 
     "id": 0, 
     "type": "suppliers", 
     "name": "Red Network Energy LTD" 
    } 
} 

。実行する場合:

SupplierResponseTest supplierResponse = gson.fromJson(supplierJsonResponse, SupplierResponseTest.class);

を私はエラーを取得:

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 57 path $.data

public SupplierResponseTest sendPostRequest(String supplierName){ 


    SupplierResponseTest supplierResponse; 


    try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { 


     //Create new object 
     SupplierTest supplier = new SupplierTest(supplierName); 

     //convert to Json 
     Gson gson = new Gson(); 
     String requestBody = gson.toJson(supplier); 


     //set entity 

     HttpPost request = new HttpPost("http://127.0.0.1:4567/suppliers"); 
     StringEntity params = new StringEntity(requestBody); 
     request.addHeader("content-type", "application/json"); 
     request.setEntity(params); 
     HttpResponse result = httpClient.execute(request); 
     String supplierJsonResponse = EntityUtils.toString(result.getEntity(), "UTF-8"); 


     supplierResponse = gson.fromJson(supplierJsonResponse, SupplierResponseTest.class); 

     return supplierResponse; 








    } catch (Exception e) { 
     String status = ""; 
     System.out.println(e.getMessage()); 
     System.out.println(e.getClass()); 
     System.out.println(e.getStackTrace()); 

     status = "NOK"; 

    } 
    //return status; 
    return null; 

} 

オブジェクトは以下の通りです。あなたのクラスで

package json.responses; 
import com.google.gson.Gson; 

public class SupplierResponseTest { 

    private StatusResponseTest status; 
    private ApiVersionResponseTest jsonapi; 
    private String message; 
    private ResponseDataTest data; 


    public SupplierResponseTest(StatusResponseTest status, ApiVersionResponseTest jsonapi) { 
     this.status = status; 
     this.jsonapi = jsonapi; 

    } 
    public SupplierResponseTest(StatusResponseTest status, ApiVersionResponseTest jsonapi, String data, String message) { 
     this.status = status; 
     this.jsonapi = jsonapi; 
     this.message = message; 

     //data which needs to take into account the array of suppliers 
     try{ 
      Gson gson = new Gson(); 
      this.data = gson.fromJson(data, ResponseDataTest.class); 


     }catch(Exception e){ 
      System.out.println(e.getMessage()); 
     } 
    } 

    public StatusResponseTest getStatus() { 
     return status; 
    } 
    public ApiVersionResponseTest getJsonapi() { 
     return jsonapi; 
    } 
    public String getMessage() { 
     return message; 
    } 

    //getData which needs to take into account the array of suppliers 
    public ResponseDataTest getData() { 
     return data; 
    } 

} 
+0

Json文字列のカスタム 'JsonDeserializer'を作成する必要があります。このリンクは役立ちます(http://www.programcreek.com/java-api-examples/index.php?api=com.google.gson。 Json 'data'は配列ですが、あなたのクラスでは 'ResponseDataTest data'はJSONという言葉のオブジェクトです –

+0

'data'へのすべての参照を配列に変換しようとしましたが、同じエラーが発生します – TheMightyLlama

答えて

0

からSupplierResponseTest.java

private ResponseDataTest[] data;

private ResponseDataTest data;を交換し、あなたのJSONレスポンス 'データ' の場合と同様にprivate String status;

private StatusResponseTest status;を置き換えるには、この時JSONArray

ルックが含まれていますコード -

package com.stack.example; 

import com.google.gson.Gson; 

public class SupplierResponseTest { 

    private String status; 
    private ApiVersionResponseTest jsonapi; 
    private String message; 
    private ResponseDataTest[] data; 


    public SupplierResponseTest(String status, ApiVersionResponseTest jsonapi) { 
     this.status = status; 
     this.jsonapi = jsonapi; 

    } 
    public SupplierResponseTest(String status, ApiVersionResponseTest jsonapi, String data, String message) { 
     this.status = status; 
     this.jsonapi = jsonapi; 
     this.message = message; 

     //data which needs to take into account the array of suppliers 
     try{ 
      Gson gson = new Gson(); 
      this.data = gson.fromJson(data, ResponseDataTest[].class); 


     }catch(Exception e){ 
      System.out.println(e.getMessage()); 
     } 
    } 

    public String getStatus() { 
     return status; 
    } 
    public ApiVersionResponseTest getJsonapi() { 
     return jsonapi; 
    } 
    public String getMessage() { 
     return message; 
    } 

    //getData which needs to take into account the array of suppliers 
    public ResponseDataTest[] getData() { 
     return data; 
    } 

    @Override 
    public String toString() { 
     return "SupplierResponseTest [status=" + status + ", jsonapi=" 
      + jsonapi + ", message=" + message + ", data=" + data + "]"; 
    } 
} 

public class ApiVersionResponseTest { 

    private String version; 

    public ApiVersionResponseTest() { 
     super(); 
     // TODO Auto-generated constructor stub 
    } 

    public ApiVersionResponseTest(String version) { 
     super(); 
     this.version = version; 
    } 

    public String getVersion() { 
     return version; 
    } 

    public void setVersion(String version) { 
     this.version = version; 
    } 
} 

package com.stack.example; 

public class ResponseDataTest { 

    private int id; 
    private String type; 
    private String name; 
    public ResponseDataTest() { 
     super(); 
     // TODO Auto-generated constructor stub 
    } 
    public ResponseDataTest(int id, String type, String name) { 
     super(); 
     this.id = id; 
     this.type = type; 
     this.name = name; 
    } 
    public int getId() { 
     return id; 
    } 
    public void setId(int id) { 
     this.id = id; 
    } 
    public String getType() { 
     return type; 
    } 
    public void setType(String type) { 
     this.type = type; 
    } 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
} 

package com.stack.example; 

import com.google.gson.Gson; 

public class Stack { 


    public static void main(String[] args) { 

     String jsonResponse = "{\"status\": \"SUCCESS\",\"jsonapi\": {\"version\": \"1.0\"},\"data\": [" 
       + "{\"id\": 70,\"type\": \"suppliers\",\"name\": \"Blue Network Energy LTD\"}," 
       + "{\"id\": 71,\"type\": \"suppliers\",\"name\": \"Red Network Energy LTD\"}," 
       + "{\"id\": 72,\"type\": \"suppliers\",\"name\": \"Orange Network Energy LTD\"}," 
       + "{\"id\": 73,\"type\": \"suppliers\",\"name\": \"Green Network Energy LTD\"}]}"; 

     Gson gson = new Gson(); 

     //Parse json into a SupplierResponseTest object. 
     SupplierResponseTest supplierResponse = gson.fromJson(jsonResponse, SupplierResponseTest.class); 

     System.out.println(supplierResponse); 

    } 

} 

これはうまくいきます。

+0

残念ながら、私は同じエラーが発生します: – TheMightyLlama

+0

エラーが続きます... – TheMightyLlama

+0

@TheMightyLlama私はあなたが私の答えで追加したコードを見て何かを見逃していると思います。 –

関連する問題