【问题标题】:parse json from recipepuppy in java从java中的recippuppy解析json
【发布时间】:2015-05-09 00:35:48
【问题描述】:

我正在尝试制作一个 java 命令行工具,它可以让您输入任何食物的名称,它会为您提供有关它的更多信息,例如成分和图像链接。为了获取数据,我使用了食谱小狗 API。我已经设法将食谱小狗中的 json 转换成一个字符串,我称之为 mFoodJson。

现在我希望能够获取字符串的 json 内容并能够以类似数组的形式访问它,例如,我可以获得在食谱小狗中搜索披萨的第一个结果并返回有点像这样的成分(假设 mFoodJson 包含来自食谱小狗的 json)

String[] mFoodArray = mFoodJson.toArray();
System.out.println("Ingredients in " + mFoodQuery + ": " + mFoodArray[1].ingredients;

菜谱小狗从他们的 API 返回的 json 示例有点像这样(假设搜索的食物是“披萨”):

{
    "title":"Recipe Puppy",
    "version":0.1,
    "href":"http:\/\/www.recipepuppy.com\/",
    "results": 
    [
        {
            "title":"BBQ Chicken Pizza",
            "href":"http:\/\/www.recipezaar.com\/BBQ-Chicken-Pizza-144689",
            "ingredients":  "chicken, brown sugar, cayenne, garlic salt, green pepper, honey, italian cheese blend, salad dressing, margarine, molasses, onions, barbecue sauce, black pepper, prepared pizza crust, provolone cheese, ranch dressing, salt",
            "thumbnail":""
        },
        {
            "title":"Basic Chicago-style Pizza Recipe",
            "href":"http:\/\/www.grouprecipes.com\/65487\/basic-chicago-style-pizza.html",
            "ingredients":  "pizza, vegetable oil,cornmeal, water, flour, sausage, provolone cheese, olive oil, tomato, yeast, pepperoni, salt, salt, sugar, basil, oregano",
            "thumbnail":""
        },
        {
            "title":"BBQ'd Cheeseburger Pizza",
            "href":"http:\/\/www.recipezaar.com\/BBQd-Cheeseburger-Pizza-299376",
            "ingredients":  "barbecue sauce, cheddar cheese, onions, tomato, dill pickle, dill relish, parsley, french dressing, garlic powder, ground beef, lettuce, mayonnaise, mozzarella cheese, pizza dough, mustard",
            "thumbnail":"http:\/\/img.recipepuppy.com\/152332.jpg"
        }
    ]
}

【问题讨论】:

    标签: java arrays json


    【解决方案1】:

    不要重新发明轮子。有很多 json 解析器,例如 Jackson、Gson 或 LoganSquare。谷歌为他们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-16
      • 2017-02-20
      • 1970-01-01
      • 1970-01-01
      • 2020-09-17
      • 2011-02-20
      • 2010-12-05
      相关资源
      最近更新 更多