0

jsonを使用してデータを解析していますが、

{
"userDetails": [
    {
        "id": "186",
        "username": "manoj",
        "status": "1"
    },
    {
        "id": "189",
        "username": "anu",
        "status": "1"
    },
    {
        "id": "190",
        "username": "manojsoyal",
        "status": "1"
    },
    {
        "id": "191",
        "username": "mohit",
        "status": "1"
    },
    {
        "id": "192",
        "username": "micky",
        "status": "1"
    },
    {
        "id": "193",
        "username": "preet",
        "status": "1"
    },
    {
        "id": "194",
        "username": "mohit",
        "status": "1"
    },
    {
        "id": "195",
        "username": "mohit",
        "status": "1"
    },
    {
        "id": "196",
        "username": "harold",
        "status": "1"
    },
    {
        "id": "197",
        "username": "sukhi",
        "status": "1"
    },
    {
        "id": "198",
        "username": "harold2",
        "status": "1"
    },
    {
        "id": "199",
        "username": "sukhi",
        "status": "1"
    },
    {
        "id": "200",
        "username": "neha",
        "status": "1"
    },
    {
        "id": "201",
        "username": "jay",
        "status": "1"
    },
    {
        "id": "202",
        "username": "qwe",
        "status": "1"
    }
]

}

これが私のコードです

public class MainActivity extends Activity {

final static String URL = "this is the url";

String appUsernames[] = null;
String appUserIDs[] = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    JSONParser jParser = new JSONParser();
    JSONObject json = jParser.getJSONFromUrl(URL);

    try {
        JSONArray userDetails = json.getJSONArray("userDetails");
        for (int i = 0; i < userDetails.length(); i++) {
            JSONObject c = userDetails.getJSONObject(i);

            String name = c.getString("username");
            String id = c.getString("id");

            appUsernames = new String[userDetails.length()];
            appUserIDs = new String[userDetails.length()];

            appUsernames[i] = name;
            appUserIDs[i] = id;

            Log.d("First", appUserIDs[i] + ": " + appUsernames[i]);

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

    for (int i = 0; i < appUserIDs.length; i++) {
        Log.d("Second", appUserIDs[i] + ": " + appUsernames[i]);
    }
}

}

私の最初の log.d では、文字列配列は空ではありません。しかし、2 番目の log.d では、文字列配列の最後のインデックスを除いて null のように見えます。何が起こっている?私を助けてください。

4

1 に答える 1

0
 appUsernames = new String[userDetails.length()];
 appUserIDs = new String[userDetails.length()];

配列の作成を for ループの外に移動する

try {
        JSONArray userDetails = json.getJSONArray("userDetails");
        appUsernames = new String[userDetails.length()];
        appUserIDs = new String[userDetails.length()];
        for (int i = 0; i < userDetails.length(); i++) {
            JSONObject c = userDetails.getJSONObject(i);

            String name = c.getString("username");
            String id = c.getString("id");

            appUsernames[i] = name;
            appUserIDs[i] = id;

            Log.d("First", appUserIDs[i] + ": " + appUsernames[i]);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
于 2013-06-13T07:20:38.833 に答える