Java 如何从JSON中的子对象访问内容?
我想从以下JSON代码中获取“image_url”:Java 如何从JSON中的子对象访问内容?,java,json,Java,Json,我想从以下JSON代码中获取“image_url”: { "recipe": { "publisher": "Closet Cooking", "f2f_url": "http://food2fork.com/view/35382", "ingredients": [ "2 jalapeno peppers, cut in half lengthwise and seeded", "2 slices sour dough bread",
{
"recipe": {
"publisher": "Closet Cooking",
"f2f_url": "http://food2fork.com/view/35382",
"ingredients": [
"2 jalapeno peppers, cut in half lengthwise and seeded",
"2 slices sour dough bread",
"1 tablespoon butter, room temperature",
"2 tablespoons cream cheese, room temperature",
"1/2 cup jack and cheddar cheese, shredded",
"1 tablespoon tortilla chips, crumbled\n"
],
"source_url": "http://www.closetcooking.com/2011/04/jalapeno-popper-grilled-cheese-sandwich.html",
"recipe_id": "35382",
"image_url": "http://static.food2fork.com/Jalapeno2BPopper2BGrilled2BCheese2BSandwich2B12B500fd186186.jpg",
"social_rank": 100,
"publisher_url": "http://closetcooking.com",
"title": "Jalapeno Popper Grilled Cheese Sandwich"
}
}
使用“javajson.jar”中的库,使用以下java代码:
我可以创建JSONObject“recipe”,但是,当执行下一行时,我会收到一条错误消息,显示“image\u url”未找到
响应是我从前面的URL请求中获得的原始文本。尝试以下操作:
JSONObject json = new JSONObject(yourJsonString);
System.out.println(json.getJSONObject("recipe").get("image_url"));
这是import org.primefaces.json.JSONObject中的JSONObject代码>
此外,以下是您正在执行的方法:
JSONObject json = new JSONObject(yourJsonString);
JSONObject json2 = json.getJSONObject("recipe");
String imageUrl = json2.getString("image_url");
System.out.println(imageUrl);
我认为您只需要调试或记录一些东西来检查您的输入。
我只是做一个测试,然后得到结果
在我看来,这可能是因为你的回答和你写的不一样
- 源代码:
结果是:
{"social_rank":100,"f2f_url":"http:\/\/food2fork.com\/view\/35382","recipe_id":"35382","publisher_url":"http:\/\/closetcooking.com","image_url":"http:\/\/static.food2fork.com\/Jalapeno2BPopper2BGrilled2BCheese2BSandwich2B12B500fd186186.jpg","publisher":"Closet Cooking","ingredients":["2 jalapeno peppers, cut in half lengthwise and seeded","2 slices sour dough bread","1 tablespoon butter, room temperature","2 tablespoons cream cheese, room temperature","1\/2 cup jack and cheddar cheese, shredded","1 tablespoon tortilla chips, crumbled\n"],"title":"Jalapeno Popper Grilled Cheese Sandwich","source_url":"http:\/\/www.closetcooking.com\/2011\/04\/jalapeno-popper-grilled-cheese-sandwich.html"}
http://static.food2fork.com/Jalapeno2BPopper2BGrilled2BCheese2BSandwich2B12B500fd186186.jpg
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
/**
* @description: add your desc
* @author: walker
* @create: 2019-06-21 13:06
**/
public class JSONTest {
@Test
public void test() throws JSONException {
String json = "\t\t{\n" +
"\t\t\t\"recipe\": {\n" +
"\t\t\t\"publisher\": \"Closet Cooking\",\n" +
"\t\t\t\t\t\"f2f_url\": \"http://food2fork.com/view/35382\",\n" +
"\t\t\t\t\t\"ingredients\": [\n" +
"\t\t\t\"2 jalapeno peppers, cut in half lengthwise and seeded\",\n" +
"\t\t\t\t\t\"2 slices sour dough bread\",\n" +
"\t\t\t\t\t\"1 tablespoon butter, room temperature\",\n" +
"\t\t\t\t\t\"2 tablespoons cream cheese, room temperature\",\n" +
"\t\t\t\t\t\"1/2 cup jack and cheddar cheese, shredded\",\n" +
"\t\t\t\t\t\"1 tablespoon tortilla chips, crumbled\\n\"\n" +
" ],\n" +
"\t\t\t\"source_url\": \"http://www.closetcooking.com/2011/04/jalapeno-popper-grilled-cheese-sandwich.html\",\n" +
"\t\t\t\t\t\"recipe_id\": \"35382\",\n" +
"\t\t\t\t\t\"image_url\": \"http://static.food2fork.com/Jalapeno2BPopper2BGrilled2BCheese2BSandwich2B12B500fd186186.jpg\",\n" +
"\t\t\t\t\t\"social_rank\": 100,\n" +
"\t\t\t\t\t\"publisher_url\": \"http://closetcooking.com\",\n" +
"\t\t\t\t\t\"title\": \"Jalapeno Popper Grilled Cheese Sandwich\"\n" +
"\t\t}\n" +
"\t\t}";
JSONObject jsonObject = new JSONObject(json);
JSONObject recipe = new JSONObject(jsonObject.get("recipe").toString());
System.out.println(recipe);
System.out.println(recipe.get("image_url"));
}
}
{"social_rank":100,"f2f_url":"http:\/\/food2fork.com\/view\/35382","recipe_id":"35382","publisher_url":"http:\/\/closetcooking.com","image_url":"http:\/\/static.food2fork.com\/Jalapeno2BPopper2BGrilled2BCheese2BSandwich2B12B500fd186186.jpg","publisher":"Closet Cooking","ingredients":["2 jalapeno peppers, cut in half lengthwise and seeded","2 slices sour dough bread","1 tablespoon butter, room temperature","2 tablespoons cream cheese, room temperature","1\/2 cup jack and cheddar cheese, shredded","1 tablespoon tortilla chips, crumbled\n"],"title":"Jalapeno Popper Grilled Cheese Sandwich","source_url":"http:\/\/www.closetcooking.com\/2011\/04\/jalapeno-popper-grilled-cheese-sandwich.html"}
http://static.food2fork.com/Jalapeno2BPopper2BGrilled2BCheese2BSandwich2B12B500fd186186.jpg