2

Facebookへのリクエストから与えられたフィードを解析しようとしています。しかし、私のパーサーは多くの情報をスキップしています。以下は、コードの一部であり、解析されているものと、ifステートメントで出力されているものの例です。たとえば、「メッセージ」は見つかりません。アイデア?

List<FacebookItem> list = new ArrayList<FacebookItem>();

try {
    if (jFactory == null)
        jFactory = new JsonFactory();

    jParser = jFactory.createJsonParser(json);

    FacebookItem o = null;
    while (jParser.nextToken() != null) {
        if ("type".equals(jParser.getCurrentName())) {
            jParser.nextToken();
            o = new FacebookItem();
            o.setType(jParser.getText());
            System.out.println("Found type:   " + jParser.getText());
        }
        if ("from".equals(jParser.getCurrentName()))
            while (!"name".equals(jParser.getCurrentName())) {
                jParser.nextToken();
            }
            jParser.nextToken();
            o.getUser().setUserName(jParser.getText());
            System.out.println("Found name:   " + jParser.getText());
        }
        if ("message".equals(jParser.getCurrentName())) {
            jParser.nextToken();
            o.setText(jParser.getText());
            System.out.println("Found message:   " + jParser.getText());
        }
        if ("created_time".equals(jParser.getCurrentName())) {
            jParser.nextToken();
            o.setTimestamp(jParser.getText());
            System.out.println("Found created_time:   " + jParser.getText());
            list.add(o);
        }
    }
    jParser.close();

} catch (JsonGenerationException e) {
    e.printStackTrace();
} catch (JsonMappingException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

{"data":[{"type": "photo"、 "link": "http://www.facebook.com/photo.php?fbid=10151331660204305&set=a.299455459304.180838.197394889304&type=1&relevant_count=1"、 " from ":{" name ":" FC Barcelona "、" category ":" Professional sports team "、" id ":" 197394889304 "}、" message ":"Puyolは8週間のレイオフに直面していますhttp://bit.ly/ QXhOFE \ r \ n \ r \ nPuyol、vuit setmanes de baixa http://bit.ly/T1enma\r\n\r\nPuyol、ocho semanas de baja http://bit.ly/T0eaj8 "、" picture " :"http://photos-g.ak.fbcdn.net/hphotos-ak-snc6/246623_10151331660204305_75156416_s.jpg"、 "created_time": "2012-10-03T17:40:06 + 0000"、 "id": " 197394889304_10151331937599305 "}、{" type ":" checkin "、"link": "http://www.facebook.com/pages/Hubben-21/249474758405147"、 "from":{"name": "Andreas Rol \ u00e9n"、 "id": "575703056"}、 "メッセージ":" En timme in i turneringen och redan chipleader "、" pictures ":" http://profile.ak.fbcdn.net/static-ak/rsrc.php/v2/y5/r/j258ei8TIHu.png "、 "name": "Hubben 2.1"、 "created_time": "2012-10-03T17:24:19 + 0000"、 "id": "575703056_10151181582028057"}、{"type": "photo"、 "link": " http://www.facebook.com/photo.php?fbid=10151445544542786&set=at.10151445533587786.588547.694032785.688088336&type=1&relevant_count=1 "、" from ":{" name ":" Jens Wilhelmsson "、" id ":" 688088336 「}、」写真":" http://photos-h.ak.fbcdn.net/hphotos-ak-prn1/72091_10151445544542786_676020840_s.jpg "、" created_time ":" 2012-10-03T17:21:40 + 0000 "、" id " :"688088336_10151111148358337"}、{"type": "link"、 "from":{"name": "Eric Lindqvist"、 "id": "648057222"}、 "created_time": "2012-10-03T17:20 :52 + 0000 "、" id ":" 648057222_10151249963167223 "}created_time ":" 2012-10-03T17:20:52 + 0000 "、" id ":" 648057222_10151249963167223 "}created_time ":" 2012-10-03T17:20:52 + 0000 "、" id ":" 648057222_10151249963167223 "}

Found type:   photo
Found name:   FC Barcelona
Found name:   Andreas Rolén
Found name:   Hubben 2.1
Found created_time:   2012-10-03T17:24:19+0000
Found type:   photo
Found name:   Jens Wilhelmsson
Found name:   Eric Lindqvist
4

3 に答える 3

1

私はあなたのJSONライブラリを本当に知りませんが、AndroidはSDK自体に素晴らしいJSON APIを提供します、なぜそれを使用しないのですか?

JSONArray data = new JSONArray(jsonString);
int itemCount = data.length();
List<MyObject> list = new ArrayList<MyObject>(itemCount);

for (int i=0; i<itemCount; ++i) {
  JSONObject item = data.getJSONObject(i);

  String name = item.getString("name");
  String message = item.optString("message");

  MyObject o = new MyObject(name, message);
  list.add(o);
}

単一のアイテムのJSON解析をMyObjectクラスにカプセル化するのが最善です。

JSONArray data = new JSONArray(jsonString);
int itemCount = data.length();
List<MyObject> list = new ArrayList<MyObject>(itemCount);

for (int i=0; i<itemCount; ++i) {
  JSONObject item = data.getJSONObject(i);

  MyObject o = new MyObject();
  o.setFromJSON(item);
  list.add(o);
}
于 2012-10-03T20:14:51.857 に答える
1

私はジャクソンが本当に好きです、それは速くてとても有能です。私はストリーミングAPI(ここで使用している)を使用したことはありませんが、それは確かに難しい方法のように見えます。メモリ使用量について非常に心配していない限り、ストリーミングAPIを使用しないことをお勧めします。あなたはモバイルを使用しているので、明らかにリソースについてより多くの懸念があります。それがあなたの呼びかけです。

ストリーミングAPIを廃止する場合は、JSONにマップするJavaオブジェクトモデルを作成し、ObjectMapperを使用してそのオブジェクトモデルに文字列を読み込むのが最も簡単です。アノテーションを使用して変換をカスタマイズし、マッピングなどのフィールドの名前を変更できます。次のようなものが機能するはずです(FacebookItemが実際に定義されているものによって異なります)。

  import org.codehaus.jackson.map.DeserializationConfig.Feature;
  ....
  ObjectMapper mapper = new ObjectMapper();
  // tell it to not fail on properties that you don't have mapped, that way you
  // only have to map the fields you are interested in and can ignore the rest
  mapper.getDeserializationConfig().set(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  ItemContainer itemContainer = mapper.readValue(facebookDataJsonString, ItemContainer.class);

  // where elsewhere you have defined something like:

  class ItemContainer {
    List<FacebookItem> data;
    // getters and setters for data.
  }

以下は、コメントでeugenによって指定されたオプションです。これは、JSONにリストが含まれている場合に機能します(つまり、「データ」ラッパーがなく、代わりに次のようになり[{...},{...}]ます:

  ObjectMapper mapper = new ObjectMapper();
  // tell it to not fail on properties that you don't have mapped, that way you
  // only have to map the fields you are interested in and can ignore the rest
  mapper.getDeserializationConfig().set(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  List<FacebookItem> items = mapper.readValue(jsonString, new TypeReference<List<Stuff>>(){});

最後に、3番目のオプションは、オブジェクトマッピングがない場合に、マップに読み込むことです。そうすれば、少なくともJSONObjectやJSONArrayのものを扱う必要はありません。

  ObjectMapper mapper = new ObjectMapper();
  Map<String, Object> container = mapper.readValue(facebookDataJsonString, Map.class);
  List<Map<String,Object>> = container.get("data");
  for (Map<String,Object> map : container ) {
    System.out.println( "type is " + map.get("type"));
    System.out.println( "from is " + ((Map<String,Object>)map.get("from")).get("name"));
    System.out.println( "message is " + map.get("message"));
  }
于 2012-10-03T20:24:43.783 に答える
-1

まず第一に-JSON文字列は不完全です-最後に]}がありません。アタッチすると、costomで構築されたJSONパーサーは次の結果を取得します。

  data:
    id=197394889304_10151331937599305
    picture=http://photos-g.ak.fbcdn.net/hphotos-ak-snc6/246623_10151331660204305_75156416_s.jpg
    message=Puyol faces eight week layoff http://bit.ly/QXhOFE\r\n\r\nPuyol, vuit setmanes de baixa http://bit.ly/T1enma\r\n\r\nPuyol, ocho semanas de baja http://bit.ly/T0eaj8
    link=http://www.facebook.com/photo.php?fbid=10151331660204305&set=a.299455459304.180838.197394889304&type=1&relevant_count=1
    from:
      id=197394889304
      category=Professional sports team
      name=FC Barcelona
    created_time=2012-10-03T17:40:06+0000
    type=photo
    id=575703056_10151181582028057
    picture=http://profile.ak.fbcdn.net/static-ak/rsrc.php/v2/y5/r/j258ei8TIHu.png
    message=En timme in i turneringen och redan chipleader
    name=Hubben 2.1
    link=http://www.facebook.com/pages/Hubben-21/249474758405147
    from:
      id=575703056
      name=Andreas Rol\u00e9n
    created_time=2012-10-03T17:24:19+0000
    type=checkin
    id=688088336_10151111148358337
    picture=http://photos-h.ak.fbcdn.net/hphotos-ak-prn1/72091_10151445544542786_676020840_s.jpg
    link=http://www.facebook.com/photo.php?fbid=10151445544542786&set=at.10151445533587786.588547.694032785.688088336&type=1&relevant_count=1
    from:
      id=688088336
      name=Jens Wilhelmsson
    created_time=2012-10-03T17:21:40+0000
    type=photo
    id=648057222_10151249963167223
    from:
      id=648057222
      name=Eric Lindqvist
    created_time=2012-10-03T17:20:52+0000
    type=link

今の私の質問-ここではすべて大丈夫ですか?エラーはありますか?

于 2013-09-06T21:06:22.717 に答える