从复杂JSON文件中获取值(一个对象中有多个对象)
我试图从一个JSON文件中获取一个值,但是它是一个复杂的JSON文件,我不知道如何处理它 该值位于一个对象内部,即另一个对象内部,该对象位于另一个对象内部 我想从JSON文件中检索的值是位于对象“metric”内的“value”和“unit”。我已经能够成功地检索“name”值 JSON文件:从复杂JSON文件中获取值(一个对象中有多个对象),json,parsing,jsonparser,jsonobjectrequest,Json,Parsing,Jsonparser,Jsonobjectrequest,我试图从一个JSON文件中获取一个值,但是它是一个复杂的JSON文件,我不知道如何处理它 该值位于一个对象内部,即另一个对象内部,该对象位于另一个对象内部 我想从JSON文件中检索的值是位于对象“metric”内的“value”和“unit”。我已经能够成功地检索“name”值 JSON文件: { "ingredients": [ { "name": "breakfast sausage", "image": "breakfast-sausage-li
{
"ingredients": [
{
"name": "breakfast sausage",
"image": "breakfast-sausage-links.jpg",
"amount": {
"metric": {
"value": 226.796,
"unit": "g"
},
"us": {
"value": 8,
"unit": "ounces"
}
}
},
我还将包括解析JSON的方法:
private void jsonParse(){
String url = "https://api.spoonacular.com/recipes/"+ rMealId + "/ingredientWidget.json?&apiKey=da7dd16a704f4552b70a96c1e9641b08";
RequestQueue requestQueue = Volley.newRequestQueue(this);
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("ingredients");
for(int i = 0; i < jsonArray.length(); i++){
JSONObject ingredients = jsonArray.getJSONObject(i);
String iName = ingredients.getString("name");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Log.e("Volley", error.toString());
}
});
requestQueue.add(request);
}
private void jsonParse(){
字符串url=”https://api.spoonacular.com/recipes/“+rmalid+”/ingredientWidget.json?&apiKey=da7dd16a704f4552b70a96c1e9641b08”;
RequestQueue RequestQueue=Volley.newRequestQueue(this);
JsonObjectRequest=新的JsonObjectRequest(request.Method.GET,url,null,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
JSONArray JSONArray=response.getJSONArray(“配料”);
for(int i=0;i
只关注json字符串,试试以下方法:
let ings =
{
"ingredients": [
{
"name": "breakfast sausage",
"image": "breakfast-sausage-links.jpg",
"amount": {
"metric": {
"value": 226.796,
"unit": "g"
},
"us": {
"value": 8,
"unit": "ounces"
}
}
}
]
}
let jp = require('jsonpath');
let eat = jp.query(ings, '$..metric.*');
console.log(eat);
输出:
[226.796,'g']
你能使用jsonpath吗?嗨,杰克,不,我从来没有听说过jsonpath,你知道我会怎么做吗,或者是否有任何基于此的教程或相关问题?看看这里:如果你感兴趣,我可以向你展示如何使用你在问题中的json。你好,杰克,是的,这将是非常有帮助的,因为我真的不知道该做什么嗨,杰克,非常感谢你,但是你可能不知道如何在Java中做到这一点?我使用java创建了我的代码,因此不熟悉您使用的语言used@DearbhlaMcMullen-恐怕不行。如果我从http url获取JSON文件,JSON解析是否仍然有效?此外,jsonFile中还有其他JSON对象,我只是获取了JSON文件的一个片段。不用担心,谢谢您的帮助:)@DearbhlaMcMullen-JSON字符串的源与jsonpath无关;但是您需要能够用正确的json格式完整地搜索出这些字符串。。。祝你好运