如何从JSON中的子对象访问内容?

时间:2019-06-20 22:05:24

标签: 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",
      "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"
  }
}

使用以下Java代码,使用“ java-json.jar”中的库:

import org.json.JSONException;
import org.json.JSONObject;

JSONObject myResponse = new JSONObject(response.toString());
JSONObject recipe = new JSONObject(myResponse.getJSONObject("recipe"));
ImageURL = recipe.getString("image_url");

我可以创建JSONObject“配方”,但是,当执行下一行时,我收到一条错误消息,指出未找到“ image_url”。

响应是我从先前的URL请求获得的原始文本。

2 个答案:

答案 0 :(得分:1)

尝试一下:

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);

答案 1 :(得分:1)

我认为您只需要调试或记录一些内容即可检查您的输入。 我只是做一个测试并得到结果。

我认为,可能是因为您的回复不像您写的那样。

  • 源代码:
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