2016-04-10 7 views
-2

Мне нужна помощь в анализе этого объекта json. Мой текущий код Java не разборе, что я хочу, это только разборе время приготовления:JSON Parse in JAVA android

"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-код:

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 ... 

но все, что я получаю в своем журнале, - это время приготовления, а не описание.

ответ

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.

Попробуйте это:

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

, что является правильным, и из-за этого food.Get.getString получает время приготовления пищи, но теперь мне нужно, чтобы получить еще одного ребенка, и он не работает –

+0

@Evan кодирования нормально, вы понял? Ваш код не может проанализировать дочерний рецепт ('foodGet.getJSONObject (« recipe »);'), потому что он не существует в объекте 'foodGet' (или в json-string' builder.toString() ') –

+0

, поэтому я должен просто объявите его там, а потом я смогу? или что –