2016-04-10 17 views
-2

私はこのjsonオブジェクトを解析するのに助けが必要です。JAONのJSON構文解析android

JSONObject recipe = new JSONObject(buffer.toString()); 
String cooking_time = recipe.getString("cooking_time_min"); 
Log.d("cooking_time", cooking_time); 
JSONObject directions = recipe.getJSONObject("directions"); 
JSONObject direction = directions.getJSONObject("direction"); 
String direction_description = direction.getString("direction_description"); 
Log.d("desc", direction_description); 
JSONObject Ingredients = recipe.getJSONObject("ingredients"); 
JSONObject ingredient = Ingredients.getJSONObject("ingredient"); 

所望の出力:

buffer.tostring is = E/Buffer: {"cooking_time_min":"15","directions":{"direction":[{"direction_description":"Preheat oven to 390 °F (2 ... 

が、これは私のJavaコードが

"cooking_time_min":"15", 
"directions":{ 
    "direction":[ 
    { 
     "direction_description":"Preheat oven to 390 °F (200 °C).", 
     "direction_number":"1" 
    }, 
    { 
     "direction_description":"Mix together minced lemon rind, lemon juice, chopped parsley, salt and cayenne.", 
     "direction_number":"2" 
    }, 
    { 
     "direction_description":"Rub snapper filets generously with mixture and place in baking dish.", 
     "direction_number":"3" 
    }, 
    { 
     "direction_description":"Place fish in oven and bake for about 10-15 minutes.", 
     "direction_number":"4" 
    } 
    ] 
}, 

です:私の現在のJavaコードは、それが唯一の調理時間を解析しています、私が欲しいものをパースされていません私がログに記録しているのは調理時間であり、説明ではありません。

答えて

0

私はJSONをフォーマットさ:

{ 
 
    "recipe": { 
 
    "cooking_time_min": "15", 
 
    "directions": { 
 
     "direction": { 
 
     "direction_description": "Preheat oven to 390 °F (200 °C).", 
 
     "direction_number": "1" 
 
     } 
 
    }, 
 
    "ingredients": { 
 
     "ingredient": { 
 
     "food_id": "38065", 
 
     "food_name": "Snapper (Fish) (Mixed Species)", 
 
     "ingredient_description": "1 1\/2 lbs snapper fillets", 
 
     "ingredient_url": "http:\/\/www.fatsecret.com\/calories-nutrition\/usda\/snapper-(fish)-(mixed-species)?portionid=47968&portionamount=1.500", 
 
     "measurement_description": "lb", 
 
     "number_of_units": "1.500", 
 
     "serving_id": "47968" 
 
     } 
 
    }, 
 
    "number_of_servings": "4", 
 
    "preparation_time_min": "5", 
 
    "rating": "4", 
 
    "recipe_categories": { 
 
     "recipe_category": { 
 
     "recipe_category_name": "Seafood", 
 
     "recipe_category_url": "http:\/\/www.fatsecret.com\/recipes\/collections\/ingredients\/seafood\/Default.aspx" 
 
     } 
 
    }, 
 
    "recipe_description": "Healthy fish with a tasty sauce.", 
 
    "recipe_id": "91", 
 
    "recipe_images": { 
 
     "recipe_image": "http:\/\/www.fatsecret.com\/static\/recipe\/bf0c5912-9cf8-4e7a-b07a-6703c4b77082.jpg" 
 
    }, 
 
    "recipe_name": "Baked Lemon Snapper", 
 
    "recipe_types": { 
 
     "recipe_type": "Main Dish" 
 
    }, 
 
    "recipe_url": "http:\/\/www.fatsecret.com\/recipes\/baked-lemon-snapper\/Default.aspx", 
 
    "serving_sizes": { 
 
     "serving": { 
 
     "calcium": "6", 
 
     "calories": "177", 
 
     "carbohydrate": "2.23", 
 
     "cholesterol": "63", 
 
     "fat": "2.32", 
 
     "fiber": "0.6", 
 
     "iron": "3", 
 
     "monounsaturated_fat": "0.436", 
 
     "polyunsaturated_fat": "0.788", 
 
     "potassium": "752", 
 
     "protein": "35.10", 
 
     "saturated_fat": "0.490", 
 
     "serving_size": "1 serving", 
 
     "sodium": "692", 
 
     "sugar": "0.58", 
 
     "trans_fat": "0", 
 
     "vitamin_a": "8", 
 
     "vitamin_c": "32" 
 
     } 
 
    } 
 
    } 
 
}

今ではrecipe、ルートの最初のシングル子供であることは明らかです。 このコードはJSONを読み取ります

JSONObject foodGet = new JSONObject(builder.toString()); 

そして、あなたは(cooking_time)「それだけで調理時間を解析しています」という、私たちに語った:

String cooking_time = foodGet.getString("cooking_time_min"); 

あなたが調理時間を取得した場合、その後、あなたはありませんこのjsonファイルを解析すると、ルートオブジェクトには子がありませんcooking_time_min

foodGetは、子供recipeを参照していると仮定します。あなたは、そのせいでfood.Get.getStringの正しい、と

JSONObject recipe = new JSONObject(builder.toString()); 
String cooking_time = recipe.getString("cooking_time_min"); 
JSONObject directions = recipe.getJSONObject("directions"); 
JSONObject direction = directions.getJSONObject("direction"); 
String direction_description = direction.getString("direction_description"); 
JSONObject Ingredients = recipe.getJSONObject("ingredients"); 
JSONObject ingredient = Ingredients.getJSONObject("ingredient"); 
... 
+0

は、調理時間を取得しますが、今、私は別の子を取得する必要がありますし、[OK]をコーディング@Evan –

+0

を働いていないその:

これを試してみてくださいポイントを得た?あなたのコードはオブジェクト 'foodGet'(またはjson-string' builder.toString() ')には存在しないので、子レシピ(' foodGet.getJSONObject( "recipe"); ')を解析できません –

+0

それを宣言すれば、私はできるだろうか?何か –