0

対処方法がわからない状況に遭遇しました。

サーバーからのjsonデータがあります。例:(jsonの一部を投稿しているだけなので、はい、jsonは有効です)

    "wall_id": 889149,
    "poster_image_thumbnail": "http:\/\/www.mface.me\/images\/avatar\/thumb_62441559ddb1dda7513d0f94.jpg",
    "post_type": "profile",
    "post_content": [{
        "text": "",
        "images_count": 1,
        "images": ["https:\/\/fbcdn-sphotos-a-a.akamaihd.net\/hphotos-ak-ash4\/227408_475848819113499_663318592_n.jpg"]
    }]

このjsonデータを保存するクラスを作成しました

public class feedFormat{
    Integer wall_id;
    String poster_image_thumbnail;
    String post_type;
    String post_content;
}

上記の例のように、 post_contentが空または配列になる場合があります。私はpost_contentをfeedFormatの文字列として宣言しました。これは明らかにキャスト例外をスローしています(配列を文字列に変換しますか?)。

JSONObjectがそれを文字列として読み取り、後でそこから配列に変換することを期待していましたが、そのようには見えません。

文字列または配列を動的に処理するにはどうすればよいですか?配列の場合は、分解する必要があります。

このアプリをIOSからAndroidに移植していますが、IOSには任意のクラスの「id」オブジェクトがあります。クラスがNSSTringまたはNSArrayであるかどうかを確認し、そこから取得します。ここJavaでは、それをどのように処理するかがわかりません。

どんな提案でも大歓迎です

4

5 に答える 5

1

JSON配列が空の場合、次のようになります。

"post_content": []

その後、0サイズであるという特殊性を備えた配列のままになります。

次に、JSON配列を直接解析して、たとえばArrayList>のように、サイズに関係なく適切なデータ構造にすることをお勧めします。これで、JSON配列のすべてのアイテムを確認し、アイテムごとに、配列リストに新しいHashMapを追加できるようになります。すべてのハッシュマップには、キー値のペアが含まれます。

ただし、JSONをよく理解している場合は、常に3つの要素の配列であり、3番目の要素自体が配列であり、そのサイズは属性images_countで指定されます。これはあまり良くありません。JSON構造は次のようになります。

"post_content": {
    "text": "",
    "images": [
        "https://fbcdn-sphotos-a-a.akamaihd.net/hphotos-ak-ash4/227408_475848819113499_663318592_n.jpg"
    ]
}

画像は配列であるため、サイズを簡単に取得できます。

于 2012-10-05T09:53:17.547 に答える
0

次のようなものを使用できます。

String data= "wall_id": 889149,
"poster_image_thumbnail": "http:\/\/www.mface.me\/images\/avatar\/thumb_62441559ddb1dda7513d0f94.jpg",
"post_type": "profile",
"post_content": [{
    "text": "",
    "images_count": 1,
    "images": ["https:\/\/fbcdn-sphotos-a-a.akamaihd.net\/hphotos-ak-ash4\/227408_475848819113499_663318592_n.jpg"]
}]

JSONArray jArray=data.getJSONArray("post_content");
for(int i=0; i<jArray.length(); i++)
{
JSONObject jObj=jArray.getJSONObject(i);
String text=jObj.getString("text");
int images_count=jObj.getInt("images_count");
String images=jObj.getInt("images");
}
于 2012-10-05T10:06:00.763 に答える
0

JSONObjecthas(String key)キーのマッピングがあるisNull(String key)かどうかをチェックし、特定のキーがであるかどうかをチェックする関数がありますnull。読む前にこれらを使用してキーを確認してください。

于 2012-10-05T09:53:29.170 に答える
0

このように確認できますjsonobject.has("post_content")

if(jsonobject.has("post_content")) {
   /// read array and do remaining stuff
}else {
  // if not read another strings and put post_content as null.
}
于 2012-10-05T09:54:31.633 に答える
0
public class FeedFormat{
    Integer wall_id;
    String poster_image_thumbnail;
    String post_type;
    JSONArray post_content;
}

feedFormat toto = new feedFormat();
toto.post_content = yourJsonObject.getJsonArray("post_content");

これはあなたがやりたいことをする最も簡単な方法です。別の方法は、別のクラスを作成することです。

public class FeedFormat{
    Integer wall_id;
    String poster_image_thumbnail;
    String post_type;
    ArrayList<PostContent> post_content = new ArrayList<PostContent>();
}

public class PostContent {
    String text;
    Integer imageCount;
    ArrayList<String> images = new ArrayList<String>();
}

これにより、JSONObject / JSONArrayを使用する代わりに、各投稿コンテンツを特定のオブジェクトに処理できます。

于 2012-10-05T09:58:04.153 に答える