0

我有这样的json

array (
  'status' => 'OK',
  'categories' => 
  array (
    'Sepeda' => 
    array (
      83 => 'Sepeda MTB',
      'Fullbike' => 
      array (
        370 => 'MTB',
        371 => 'Roadbike',
        374 => 'Fixie',
        375 => 'Sepeda Lipat',
        378 => 'City Bike',
        380 => 'BMX',
        382 => 'Onthel',
      ),

我想问一下,如何获得“Fullbike”的代码?谢谢你的建议,真的需要帮助,谢谢

4

2 回答 2

0

Checkout the PHP docs to get your array to JSON: PHP:json_encode

Alternatively, could parse the PHP array as it is, but that doesn't sound like fun.

Second checkout the Android JSON handling docs to see how to process JSON with Android. That's best done with JSONObject

Here's an example

String getJsonThingy(String rawJson) {
String strFullBike = "";
        try {
            JSONObject json = new JSONObject(rawJson);
            JSONObject jsonCategories = json.getJSONObject("categories");
            JSONObject jsonSepeda = jsonCategories.getJSONObject("Sepeda");
            strFullBike = jsonSepeda.getString("Fullbike");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return strFullBike;

I'd like to point out that this code isn't what you want your final product to look like -- it's a start.

Also, check out these related SO questions to help you get a better idea of what you should be looking for.

Final note: It may not be a bad idea for your Android client to ask your server for specific parts of your array, as seeing how your array is nested.

于 2013-05-21T03:32:02.530 回答
0
try {
     //get Full Bike Json object first
     JSONObject jsonObject = response.getJSONObject("categories").getJSONObject("Sepeda").getJSONObject("Fullbike");
     //Get inner values like this now
      String mtb = jsonObject.getString("370");
      Log.e("Response", mtb);           
} catch (JSONException e) {
      e.printStackTrace(); 
}
于 2013-05-21T03:40:24.117 回答