2016-07-28 5 views
0

jsonの出力を解析するためにjacksonを使用しているアプリケーションを作成しました(http REST呼び出しを行うためにAndroid用のSpringを使用しています)。json出力でオブジェクトを取得するときに問題が発生する

これは、次のREST呼び出しの出力です:

public class HttpRequestTask extends AsyncTask<Void, Void, RadioInfo> { 
     @Override 
     protected RadioInfo doInBackground(Void... params) { 
      try { 
       final String url = "https://api.spreaker.com/v2/users/7725967/episodes?limit=1"; 
       RestTemplate restTemplate = new RestTemplate(); 
       ObjectMapper objectMapper = new ObjectMapper(); 
       objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); 
       restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); 
       //String result = String.valueOf(restTemplate.getForObject(url, RadioInfo.class)); 
       return restTemplate.getForObject(url, RadioInfo.class); 
      } catch (Exception e) { 
       Log.e("MainActivity", e.getMessage(), e); 
      } 

      return null; 
     } 
:私のアプリケーションで

https://api.spreaker.com/v2/users/7725967/episodes?limit=1

{ 
    "response":{ 
     "items":[ 
     { 
      "episode_id":9074409, 
      "type":"RECORDED", 
      "title":"Seerah", 
      "duration":4608260, 
      "show_id":1530711, 
      "author_id":7725967, 
      "site_url":"https:\/\/www.spreaker.com\/episode\/9074409", 
      "image_url":"https:\/\/d1bm3dmew779uf.cloudfront.net\/large\/4c3b84b16338c57af8500fea88ef5313.jpg", 
      "image_original_url":"https:\/\/d3wo5wojvuv7l.cloudfront.net\/images.spreaker.com\/original\/4c3b84b16338c57af8500fea88ef5313.jpg", 
      "published_at":"2016-07-28 01:07:18", 
      "download_enabled":true, 
      "waveform_url":"https:\/\/d3770qakewhkht.cloudfront.net\/episode_9074409.gz.json?v=2GDXyK" 
     } 
     ], 
     "next_url":"https:\/\/api.spreaker.com\/v2\/users\/7725967\/episodes?filter=listenable&last_id=9074409&limit=1" 
    } 
} 

、私は次のREST呼び出しを(ここでコードがある)を作ります

エラーは次のようになります。

Could not read JSON: Unrecognized field "response" (class software.blackstone.masjidtawheedmobileradio.RadioInfo), not marked as ignorable (3 known properties: "title", "type", "episode_id"]) 
                           at [Source: buffer([email protected]).inputStream(); line: 1, column: 14] (through reference chain: software.blackstone.masjidtawheedmobileradio.RadioInfo["response"]); nested exception is com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "response" (class software.blackstone.masjidtawheedmobileradio.RadioInfo), not marked as ignorable (3 known properties: "title", "type", "episode_id"]) 
                           at [Source: buffer([email protected]).inputStream(); line: 1, column: 14] (through reference chain: software.blackstone.masjidtawheedmobileradio.RadioInfo["response"]) 
                          org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unrecognized field "response" (class software.blackstone.masjidtawheedmobileradio.RadioInfo), not marked as ignorable (3 known properties: "title", "type", "episode_id"]) 
                           at [Source: buffer([email protected]).inputStream(); line: 1, column: 14] (through reference chain: software.blackstone.masjidtawheedmobileradio.RadioInfo["response"]); nested exception is com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "response" (class software.blackstone.masjidtawheedmobileradio.RadioInfo), not marked as ignorable (3 known properties: "title", "type", "episode_id"]) 
                           at [Source: buffer([email protected]).inputStream(); line: 1, column: 14] (through reference chain: software.blackstone.masjidtawheedmobileradio.RadioInfo["response"]) 
                           at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readInternal(MappingJackson2HttpMessageConverter.java:126) 
                           at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:147) 
                           at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:76) 
                           at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:484) 
                           at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:439) 
                           at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:237) 
                           at software.blackstone.masjidtawheedmobileradio.MainActivity$HttpRequestTask.doInBackground(MainActivity.java:180) 
                           at software.blackstone.masjidtawheedmobileradio.MainActivity$HttpRequestTask.doInBackground(MainActivity.java:170) 

これを修正するにはどうすればよいですか?

UPDATE:

ここ

がRadioInfoクラスです:

パブリッククラスRadioInfo {

private String episode_id; 
private String type; 
private String title; 

public String getId() { 
    return this.episode_id; 
} 

public String getType() { 
    return this.type; 
} 

public String getTitle() { 
    return this.title; 
} 

}

+2

ショー 'RadioInfo'クラス – Divers

+0

@Divers私はどのように私はJSONObjectにしたい項目を呼び出すんRadioInfoクラス – ironmantis7x

答えて

1

、あなたのPOJOクラスは次のようになります。

public class RadioInfo { 
    private Response response; 
    // getters setters 
} 

public class Item { 
    private Integer episodeId; 
    private String type; 
    private String title; 
    //getter setters 
} 



public class Response { 
    private List<Item> items = new ArrayList<Item>(); 
    private String nextUrl; 
    //getters setters 
} 
+0

大丈夫これで私を歩いてください....私はここで本当に単純なものを見逃しています... – ironmantis7x

+0

@ ironmantis7xはい、お互いに一致しないPOJOのJSONを解析しようとしています。 – Divers

+0

それぞれのクラスファイルは別々です(私はこれがあなたが言っていると思っていますが、私は確信したいと思います)。 – ironmantis7x

0

私は見ての通り、あなたはJSONObjectという名前を取得しようとしている - 「応答" "応答 "はJSONArrayです。 JSONObjectの名前は「アイテム」です。あなたのJSONを1として

+0

を追加しましたか? – ironmantis7x

関連する問題