繁体   English   中英

json的GSON返回null

[英]GSON from json returning null

我正在android studio 3.1.4中制作一个android移动应用程序,并且当前正在尝试使用GSON.fromjson将返回的数据转换为对象。 我已经创建了供GSON使用的类,但不确定是否正确。 我觉得这可能是个问题,但我不知道我要去哪里。

这是课程

package com.example.sam.food_u_cation;

import java.util.List;

public class apiDataClass {

String q;
int from;
int to;
params params;
List<hits> hits;

public class params {
    List<String> sane;
    List<String> q;
    List<String> app_key;
    List<String> from;
    List<String> to;
    List<String> diet;
    List<String> health;
    List<String> calories;
    List<String> app_id;
    boolean more;
    int count;
}

public class hits {

    recipes recipes;
    public class recipes{

        recipe recipe;
        transient boolean bookmarked;
        transient boolean bought;

        public class recipe {
            String uri;
            String label;
            String image;
            String source;
            String url;
            String shareAs;
            double yield;
            List<String> dietLabels;
            List<String> healthLabels;
            transient List<String> cautions;
            transient List<String> ingredientLines;
            List<ingredients> ingredients;
            transient double calories;
            double totalWeight;
            double totalTime;
            totalNutrients totalNutrients;
            transient totalDaily totalDaily;
            transient List<digestInfo> digestInfo;

            public class ingredients {
                String label;
                double weight;
            }

            public class totalNutrients {
                kcal kcal;
                fat fat;
                transient fasat fasat;
                transient fatrn fatrn;
                transient fams fams;
                transient fapu fapu;
                carbs carbs;
                transient fibtg fibtg;
                transient sugar sugar;
                protein protein;
                transient chole chole;
                transient na na;
                transient ca ca;
                transient mg mg;
                transient k k;
                transient fe fe;
                transient zn zn;
                transient p p;
                transient vita_rae vita_rae;
                transient vitc vitc;
                transient thia thia;
                transient ribf ribf;
                transient nia nia;
                transient vitb6a vitb6a;
                transient foldfe foldfe;
                transient folfd folfd;
                transient vitb12 vitb12;
                transient vitd vidd;
                transient tocpha tocpha;
                transient vitk1 vitk1;

                public class kcal {
                    String label;
                    double quantity;
                    String unit;
                }

                public class fat {
                    String label;
                    double quantity;
                    String unit;
                }

                public class fasat {
                    String label;
                    double quantity;
                    String unit;
                }

                public class fatrn {
                    String label;
                    double quantity;
                    String unit;
                }

                public class fams {
                    String label;
                    double quantity;
                    String unit;
                }

                public class fapu {
                    String label;
                    double quantity;
                    String unit;
                }

                public class carbs {
                    String label;
                    double quantity;
                    String unit;
                }

                public class fibtg {
                    String label;
                    double quantity;
                    String unit;
                }

                public class sugar {
                    String label;
                    double quantity;
                    String unit;
                }

                public class protein {
                    String label;
                    double quantity;
                    String unit;
                }

                public class chole {
                    String label;
                    double quantity;
                    String unit;
                }

                public class na {
                    String label;
                    double quantity;
                    String unit;
                }

                public class ca {
                    String label;
                    double quantity;
                    String unit;
                }

                public class mg {
                    String label;
                    double quantity;
                    String unit;
                }

                public class k {
                    String label;
                    double quantity;
                    String unit;
                }

                public class fe {
                    String label;
                    double quantity;
                    String unit;
                }

                public class zn {
                    String label;
                    double quantity;
                    String unit;
                }

                public class p {
                    String label;
                    double quantity;
                    String unit;
                }

                public class vita_rae {
                    String label;
                    double quantity;
                    String unit;
                }

                public class vitc {
                    String label;
                    double quantity;
                    String unit;
                }

                public class thia {
                    String label;
                    double quantity;
                    String unit;
                }

                public class ribf {
                    String label;
                    double quantity;
                    String unit;
                }

                public class nia {
                    String label;
                    double quantity;
                    String unit;
                }

                public class vitb6a {
                    String label;
                    double quantity;
                    String unit;
                }

                public class foldfe {
                    String label;
                    double quantity;
                    String unit;
                }

                public class folfd {
                    String label;
                    double quantity;
                    String unit;
                }

                public class vitb12 {
                    String label;
                    double quantity;
                    String unit;
                }

                public class vitd {
                    String label;
                    double quantity;
                    String unit;
                }

                public class tocpha {
                    String label;
                    double quantity;
                    String unit;
                }

                public class vitk1 {
                    String label;
                    double quantity;
                    String unit;
                }
            }

            public class totalDaily extends totalNutrients {
            }

            public class digestInfo {
                String label;
                String tag;
                String schemaOrgTag;
                double total;
                boolean hasRDI;
                double daily;
                String unit;
                List<digestInfo2> digestInfo2;

                public class digestInfo2 {
                    String label;
                    String tag;
                    String schemaOrgTag;
                    double total;
                    boolean hasRDI;
                    double daily;
                    String unit;
                }
            }
        }


    }

}
}

这是我得到的json数据

 { "q" : "", "from" : 0, "to" : 1, "params" : { "sane" : [ ], "q" : [ "" ], "app_key" : [ "{PRIVACY}" ], "from" : [ "0" ], "to" : [ "1" ], "diet" : [ "low-carb" ], "app_id" : [ "{PRIVACY}" ] }, "more" : true, "count" : 227806, "hits" : [ { "recipe" : { "uri" : "http://www.edamam.com/ontologies/edamam.owl#recipe_9b5945e03f05acbf9d69625138385408", "label" : "Pressure-Cooker Octopus Recipe", "image" : "https://www.edamam.com/web-img/c69/c6922ad323db66d90b7d2c8d3c9cd3c4.jpg", "source" : "Serious Eats", "url" : "http://www.seriouseats.com/recipes/2016/06/pressure-cooker-octopus-recipe.html", "shareAs" : "http://www.edamam.com/recipe/pressure-cooker-octopus-recipe-9b5945e03f05acbf9d69625138385408/-/low-carb", "yield" : 6.0, "dietLabels" : [ "High-Protein", "Low-Fat", "Low-Carb" ], "healthLabels" : [ "Sugar-Conscious", "Peanut-Free", "Tree-Nut-Free", "Alcohol-Free" ], "cautions" : [ ], "ingredientLines" : [ "1 (2 1/2-pound; 1kg) whole octopus, rinsed well (including inside head cavity)", "Kosher salt" ], "ingredients" : [ { "text" : "1 (2 1/2-pound; 1kg) whole octopus, rinsed well (including inside head cavity)", "weight" : 1359.0 }, { "text" : "Kosher salt", "weight" : 8.154 } ], "calories" : 1114.3799999999999, "totalWeight" : 1359.0893423809277, "totalTime" : 25.0, "totalNutrients" : { "ENERC_KCAL" : { "label" : "Energy", "quantity" : 1114.3799999999999, "unit" : "kcal" }, "FAT" : { "label" : "Fat", "quantity" : 14.1336, "unit" : "g" }, "FASAT" : { "label" : "Saturated", "quantity" : 3.08493, "unit" : "g" }, "FAMS" : { "label" : "Monounsaturated", "quantity" : 2.20158, "unit" : "g" }, "FAPU" : { "label" : "Polyunsaturated", "quantity" : 3.24801, "unit" : "g" }, "CHOCDF" : { "label" : "Carbs", "quantity" : 29.898000000000003, "unit" : "g" }, "PROCNT" : { "label" : "Protein", "quantity" : 202.6269, "unit" : "g" }, "CHOLE" : { "label" : "Cholesterol", "quantity" : 652.3199999999999, "unit" : "mg" }, "NA" : { "label" : "Sodium", "quantity" : 3160.32732, "unit" : "mg" }, "CA" : { "label" : "Calcium", "quantity" : 720.2914421714227, "unit" : "mg" }, "MG" : { "label" : "Magnesium", "quantity" : 407.70089342380925, "unit" : "mg" }, "K" : { "label" : "Potassium", "quantity" : 4756.5071473904745, "unit" : "mg" }, "FE" : { "label" : "Iron", "quantity" : 72.02729482985707, "unit" : "mg" }, "ZN" : { "label" : "Zinc", "quantity" : 22.831289342380927, "unit" : "mg" }, "P" : { "label" : "Phosphorus", "quantity" : 2527.74, "unit" : "mg" }, "VITA_RAE" : { "label" : "Vitamin A", "quantity" : 611.55, "unit" : "µg" }, "VITC" : { "label" : "Vitamin C", "quantity" : 67.95, "unit" : "mg" }, "THIA" : { "label" : "Thiamin (B1)", "quantity" : 0.4077, "unit" : "mg" }, "RIBF" : { "label" : "Riboflavin (B2)", "quantity" : 0.5436, "unit" : "mg" }, "NIA" : { "label" : "Niacin (B3)", "quantity" : 28.539, "unit" : "mg" }, "VITB6A" : { "label" : "Vitamin B6", "quantity" : 4.892399999999999, "unit" : "mg" }, "FOLDFE" : { "label" : "Folate equivalent (total)", "quantity" : 217.44, "unit" : "µg" }, "FOLFD" : { "label" : "Folate (food)", "quantity" : 217.44, "unit" : "µg" }, "VITB12" : { "label" : "Vitamin B12", "quantity" : 271.8, "unit" : "µg" }, "TOCPHA" : { "label" : "Vitamin E", "quantity" : 16.308, "unit" : "mg" }, "VITK1" : { "label" : "Vitamin K", "quantity" : 1.359, "unit" : "µg" } }, "totalDaily" : { "ENERC_KCAL" : { "label" : "Energy", "quantity" : 55.718999999999994, "unit" : "%" }, "FAT" : { "label" : "Fat", "quantity" : 21.744, "unit" : "%" }, "FASAT" : { "label" : "Saturated", "quantity" : 15.42465, "unit" : "%" }, "CHOCDF" : { "label" : "Carbs", "quantity" : 9.966000000000001, "unit" : "%" }, "PROCNT" : { "label" : "Protein", "quantity" : 405.25380000000007, "unit" : "%" }, "CHOLE" : { "label" : "Cholesterol", "quantity" : 217.43999999999997, "unit" : "%" }, "NA" : { "label" : "Sodium", "quantity" : 131.68030499999998, "unit" : "%" }, "CA" : { "label" : "Calcium", "quantity" : 72.02914421714227, "unit" : "%" }, "MG" : { "label" : "Magnesium", "quantity" : 97.07164129138316, "unit" : "%" }, "K" : { "label" : "Potassium", "quantity" : 101.20227973171222, "unit" : "%" }, "FE" : { "label" : "Iron", "quantity" : 400.1516379436504, "unit" : "%" }, "ZN" : { "label" : "Zinc", "quantity" : 207.5571758398266, "unit" : "%" }, "P" : { "label" : "Phosphorus", "quantity" : 361.10571428571427, "unit" : "%" }, "VITA_RAE" : { "label" : "Vitamin A", "quantity" : 67.94999999999999, "unit" : "%" }, "VITC" : { "label" : "Vitamin C", "quantity" : 75.5, "unit" : "%" }, "THIA" : { "label" : "Thiamin (B1)", "quantity" : 33.975, "unit" : "%" }, "RIBF" : { "label" : "Riboflavin (B2)", "quantity" : 41.815384615384616, "unit" : "%" }, "NIA" : { "label" : "Niacin (B3)", "quantity" : 178.36875, "unit" : "%" }, "VITB6A" : { "label" : "Vitamin B6", "quantity" : 376.3384615384615, "unit" : "%" }, "FOLDFE" : { "label" : "Folate equivalent (total)", "quantity" : 54.36, "unit" : "%" }, "VITB12" : { "label" : "Vitamin B12", "quantity" : 11325.0, "unit" : "%" }, "TOCPHA" : { "label" : "Vitamin E", "quantity" : 108.72, "unit" : "%" }, "VITK1" : { "label" : "Vitamin K", "quantity" : 1.1325, "unit" : "%" } }, "digest" : [ { "label" : "Fat", "tag" : "FAT", "schemaOrgTag" : "fatContent", "total" : 14.1336, "hasRDI" : true, "daily" : 21.744, "unit" : "g", "sub" : [ { "label" : "Saturated", "tag" : "FASAT", "schemaOrgTag" : "saturatedFatContent", "total" : 3.08493, "hasRDI" : true, "daily" : 15.42465, "unit" : "g" }, { "label" : "Trans", "tag" : "FATRN", "schemaOrgTag" : "transFatContent", "total" : 0.0, "hasRDI" : false, "daily" : 0.0, "unit" : "g" }, { "label" : "Monounsaturated", "tag" : "FAMS", "schemaOrgTag" : null, "total" : 2.20158, "hasRDI" : false, "daily" : 0.0, "unit" : "g" }, { "label" : "Polyunsaturated", "tag" : "FAPU", "schemaOrgTag" : null, "total" : 3.24801, "hasRDI" : false, "daily" : 0.0, "unit" : "g" } ] }, { "label" : "Carbs", "tag" : "CHOCDF", "schemaOrgTag" : "carbohydrateContent", "total" : 29.898000000000003, "hasRDI" : true, "daily" : 9.966000000000001, "unit" : "g", "sub" : [ { "label" : "Carbs (net)", "tag" : "CHOCDF.net", "schemaOrgTag" : null, "total" : 29.898000000000003, "hasRDI" : false, "daily" : 0.0, "unit" : "g" }, { "label" : "Fiber", "tag" : "FIBTG", "schemaOrgTag" : "fiberContent", "total" : 0.0, "hasRDI" : false, "daily" : 0.0, "unit" : "g" }, { "label" : "Sugars", "tag" : "SUGAR", "schemaOrgTag" : "sugarContent", "total" : 0.0, "hasRDI" : false, "daily" : 0.0, "unit" : "g" }, { "label" : "Sugars, added", "tag" : "SUGAR.added", "schemaOrgTag" : null, "total" : 0.0, "hasRDI" : false, "daily" : 0.0, "unit" : "g" } ] }, { "label" : "Protein", "tag" : "PROCNT", "schemaOrgTag" : "proteinContent", "total" : 202.6269, "hasRDI" : true, "daily" : 405.25380000000007, "unit" : "g" }, { "label" : "Cholesterol", "tag" : "CHOLE", "schemaOrgTag" : "cholesterolContent", "total" : 652.3199999999999, "hasRDI" : true, "daily" : 217.43999999999997, "unit" : "mg" }, { "label" : "Sodium", "tag" : "NA", "schemaOrgTag" : "sodiumContent", "total" : 3160.32732, "hasRDI" : true, "daily" : 131.68030499999998, "unit" : "mg" }, { "label" : "Calcium", "tag" : "CA", "schemaOrgTag" : null, "total" : 720.2914421714227, "hasRDI" : true, "daily" : 72.02914421714227, "unit" : "mg" }, { "label" : "Magnesium", "tag" : "MG", "schemaOrgTag" : null, "total" : 407.70089342380925, "hasRDI" : true, "daily" : 97.07164129138316, "unit" : "mg" }, { "label" : "Potassium", "tag" : "K", "schemaOrgTag" : null, "total" : 4756.5071473904745, "hasRDI" : true, "daily" : 101.20227973171222, "unit" : "mg" }, { "label" : "Iron", "tag" : "FE", "schemaOrgTag" : null, "total" : 72.02729482985707, "hasRDI" : true, "daily" : 400.1516379436504, "unit" : "mg" }, { "label" : "Zinc", "tag" : "ZN", "schemaOrgTag" : null, "total" : 22.831289342380927, "hasRDI" : true, "daily" : 207.5571758398266, "unit" : "mg" }, { "label" : "Phosphorus", "tag" : "P", "schemaOrgTag" : null, "total" : 2527.74, "hasRDI" : true, "daily" : 361.10571428571427, "unit" : "mg" }, { "label" : "Vitamin A", "tag" : "VITA_RAE", "schemaOrgTag" : null, "total" : 611.55, "hasRDI" : true, "daily" : 67.94999999999999, "unit" : "µg" }, { "label" : "Vitamin C", "tag" : "VITC", "schemaOrgTag" : null, "total" : 67.95, "hasRDI" : true, "daily" : 75.5, "unit" : "mg" }, { "label" : "Thiamin (B1)", "tag" : "THIA", "schemaOrgTag" : null, "total" : 0.4077, "hasRDI" : true, "daily" : 33.975, "unit" : "mg" }, { "label" : "Riboflavin (B2)", "tag" : "RIBF", "schemaOrgTag" : null, "total" : 0.5436, "hasRDI" : true, "daily" : 41.815384615384616, "unit" : "mg" }, { "label" : "Niacin (B3)", "tag" : "NIA", "schemaOrgTag" : null, "total" : 28.539, "hasRDI" : true, "daily" : 178.36875, "unit" : "mg" }, { "label" : "Vitamin B6", "tag" : "VITB6A", "schemaOrgTag" : null, "total" : 4.892399999999999, "hasRDI" : true, "daily" : 376.3384615384615, "unit" : "mg" }, { "label" : "Folate equivalent (total)", "tag" : "FOLDFE", "schemaOrgTag" : null, "total" : 217.44, "hasRDI" : true, "daily" : 54.36, "unit" : "µg" }, { "label" : "Folate (food)", "tag" : "FOLFD", "schemaOrgTag" : null, "total" : 217.44, "hasRDI" : false, "daily" : 0.0, "unit" : "µg" }, { "label" : "Folic acid", "tag" : "FOLAC", "schemaOrgTag" : null, "total" : 0.0, "hasRDI" : false, "daily" : 0.0, "unit" : "µg" }, { "label" : "Vitamin B12", "tag" : "VITB12", "schemaOrgTag" : null, "total" : 271.8, "hasRDI" : true, "daily" : 11325.0, "unit" : "µg" }, { "label" : "Vitamin D", "tag" : "VITD", "schemaOrgTag" : null, "total" : 0.0, "hasRDI" : false, "daily" : 0.0, "unit" : "µg" }, { "label" : "Vitamin E", "tag" : "TOCPHA", "schemaOrgTag" : null, "total" : 16.308, "hasRDI" : true, "daily" : 108.72, "unit" : "mg" }, { "label" : "Vitamin K", "tag" : "VITK1", "schemaOrgTag" : null, "total" : 1.359, "hasRDI" : true, "daily" : 1.1325, "unit" : "µg" } ] }, "bookmarked" : false, "bought" : false } ] } 

这是GSON电话

package com.example.sam.food_u_cation;

import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;

import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONObject;
import org.json.simple.parser.JSONParser;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Scanner;

public class getData extends AsyncTask<URL,Void,Void> {

private String inputData;

apiDataClass apiData;

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);

    apiData = new apiDataClass();
    Gson gson = new Gson();
    apiData = gson.fromJson(inputData, apiDataClass.class);

displayAPIDataPage.field.setText(apiData.hits.get(0).recipes.recipe.label);
}

protected Void doInBackground(URL... url){


    try {

        HttpURLConnection connect = (HttpURLConnection) 
url[0].openConnection();

        connect.setRequestMethod("GET");

        connect.connect();

        int code = connect.getResponseCode();

        if(code != 200){
            throw new RuntimeException("HttpResponseCode: " + code);
        }else{
            Scanner scanner = new Scanner(url[0].openStream());
            while(scanner.hasNext()){
            inputData += scanner.nextLine();
        }
        scanner.close();
    }
}
    catch(Exception ex){
    Log.d("GetAPIData",ex.toString());
}
return null;
}

}

它太长了。 如果对象具有通用对象,则使用gson可能不起作用。 TypeToken可以处理此问题。 类型type = new TypeToken>(){}。getType();

@Sam Nichols,请考虑使用与Json Response中的Json对象相同的对象名称,否则请使用@SerializedName()注释,这会将您的JsonResponse对象映射到适当的Java对象中。 我认为问题出在这里,因为您可能使用的Java对象名称与通过json响应收到的Java对象名称不同。

抱歉,这是一个愚蠢的错误,我在JSON数据中读取的方式不正确,这使gson无法正常工作。 修复它

if (code != 200) {
            throw new RuntimeException("HttpResponseCode: " + code);
        } else {
            BufferedReader br = new BufferedReader(new 
            InputStreamReader(connect.getInputStream()));

            StringBuilder sb = new StringBuilder();
            String line;

            while ((line = br.readLine()) != null) {
                sb.append(line + "\n");
            }
            br.close();

            try {
                inputData = sb.toString();
            } catch (Exception e) {

            }
        }

代替

if(code != 200){
        throw new RuntimeException("HttpResponseCode: " + code);
    }else{
        Scanner scanner = new Scanner(url[0].openStream());
        while(scanner.hasNext()){
        inputData += scanner.nextLine();
    }
    scanner.close();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM