Java jackson无法在JSON中反序列化对象数组

时间:2016-08-17 13:03:56

标签: java json jackson

当我尝试反序列化时,我遇到了Java Jackson的问题。

public class Mapper {

public static Map<String, Object> JsonToMap(String json)
        throws JsonParseException, JsonMappingException, IOException {
    // Convert the JSON recieved through POST request to a HasMap
    ObjectMapper mapper = new ObjectMapper();

    Map<String, Object> map = new HashMap<String, Object>();
    map = mapper.readValue(json, new TypeReference<Map<String, Object>>() {
    });
    return map;

}

这是JSON

{[
"UserMealDay": {
    "date": "2016-04-04",
    "slot_num": "1"
},
"Recipe": {
    "id": "82",
    "name": "Porridge with Banana & Sunflower Seeds",
    "RecipeIngredient": [{
        "id": "110263",
        "multiplier": null,
        "quantity": "50.00",
        "ingredient_id": "486",
        "ingredient_unit_id": "8",
        "recipe_id": "82",
        "Ingredient": {
            "id": "486",
            "name": "jumbo oats",
            "plural": "jumbo oats",
            "IngredientGroup": [{
                "id": "19",
                "name": "Oats",
                "IngredientGroupsIngredient": {
                    "id": "160",
                    "ingredient_group_id": "19",
                    "ingredient_id": "486",
                    "created": "0000-00-00 00:00:00",
                    "modified": "0000-00-00 00:00:00",
                    "deleted": "0",
                    "deleted_date": "0000-00-00 00:00:00"
                }
            }]
        },
        "IngredientUnit": {
            "id": "8",
            "short": "g",
            "name": "g",
            "plural": "g",
            "created": "2015-07-14 20:10:33",
            "modified": "2015-07-24 09:25:22",
            "deleted": "0",
            "deleted_date": "0000-00-00 00:00:00"
        }
    }, {
        "id": "110264",
        "multiplier": null,
        "quantity": "100.00",
        "ingredient_id": "182",
        "ingredient_unit_id": "18",
        "recipe_id": "82",
        "Ingredient": {
            "id": "182",
            "name": "cold water",
            "plural": "cold water",
            "IngredientGroup": [{
                "id": "59",
                "name": "Misc",
                "IngredientGroupsIngredient": {
                    "id": "465",
                    "ingredient_group_id": "59",
                    "ingredient_id": "182",
                    "created": "0000-00-00 00:00:00",
                    "modified": "0000-00-00 00:00:00",
                    "deleted": "0",
                    "deleted_date": "0000-00-00 00:00:00"
                }
            }, {
                "id": "121",
                "name": "water",
                "IngredientGroupsIngredient": {
                    "id": "1104",
                    "ingredient_group_id": "121",
                    "ingredient_id": "182",
                    "created": "0000-00-00 00:00:00",
                    "modified": "0000-00-00 00:00:00",
                    "deleted": "0",
                    "deleted_date": "0000-00-00 00:00:00"
                }
            }]
        },
        "IngredientUnit": {
            "id": "18",
            "short": "ml",
            "name": "ml",
            "plural": "ml",
            "created": "2015-07-14 20:11:43",
            "modified": "2015-07-24 09:42:29",
            "deleted": "0",
            "deleted_date": "0000-00-00 00:00:00"
        }
    }, {
        "id": "110265",
        "multiplier": null,
        "quantity": "100.00",
        "ingredient_id": "815",
        "ingredient_unit_id": "18",
        "recipe_id": "82",
        "Ingredient": {
            "id": "815",
            "name": "milk",
            "plural": "milk",
            "IngredientGroup": [{
                "id": "1",
                "name": "Dairy",
                "IngredientGroupsIngredient": {
                    "id": "1158",
                    "ingredient_group_id": "1",
                    "ingredient_id": "815",
                    "created": "0000-00-00 00:00:00",
                    "modified": "0000-00-00 00:00:00",
                    "deleted": "0",
                    "deleted_date": "0000-00-00 00:00:00"
                }
            }, {
                "id": "3",
                "name": "Cows Milk",
                "IngredientGroupsIngredient": {
                    "id": "1159",
                    "ingredient_group_id": "3",
                    "ingredient_id": "815",
                    "created": "0000-00-00 00:00:00",
                    "modified": "0000-00-00 00:00:00",
                    "deleted": "0",
                    "deleted_date": "0000-00-00 00:00:00"
                }
            }]
        },
        "IngredientUnit": {
            "id": "18",
            "short": "ml",
            "name": "ml",
            "plural": "ml",
            "created": "2015-07-14 20:11:43",
            "modified": "2015-07-24 09:42:29",
            "deleted": "0",
            "deleted_date": "0000-00-00 00:00:00"
        }
    }, {
        "id": "110266",
        "multiplier": null,
        "quantity": "1.00",
        "ingredient_id": "707",
        "ingredient_unit_id": "23",
        "recipe_id": "82",
        "Ingredient": {
            "id": "707",
            "name": "banana",
            "plural": "bananas ",
            "IngredientGroup": [{
                "id": "45",
                "name": "Tropical fruit",
                "IngredientGroupsIngredient": {
                    "id": "340",
                    "ingredient_group_id": "45",
                    "ingredient_id": "707",
                    "created": "0000-00-00 00:00:00",
                    "modified": "0000-00-00 00:00:00",
                    "deleted": "0",
                    "deleted_date": "0000-00-00 00:00:00"
                }
            }]
        },
        "IngredientUnit": {
            "id": "23",
            "short": "qty",
            "name": "qty",
            "plural": "qty",
            "created": "2015-07-14 20:12:19",
            "modified": "2015-07-24 09:43:13",
            "deleted": "0",
            "deleted_date": "0000-00-00 00:00:00"
        }
    }, {
        "id": "110267",
        "multiplier": null,
        "quantity": "20.00",
        "ingredient_id": "280",
        "ingredient_unit_id": "8",
        "recipe_id": "82",
        "Ingredient": {
            "id": "280",
            "name": "sunflower seeds",
            "plural": "sunflower seeds",
            "IngredientGroup": [{
                "id": "79",
                "name": "Seeds",
                "IngredientGroupsIngredient": {
                    "id": "584",
                    "ingredient_group_id": "79",
                    "ingredient_id": "280",
                    "created": "0000-00-00 00:00:00",
                    "modified": "0000-00-00 00:00:00",
                    "deleted": "0",
                    "deleted_date": "0000-00-00 00:00:00"
                }
            }]
        },
        "IngredientUnit": {
            "id": "8",
            "short": "g",
            "name": "g",
            "plural": "g",
            "created": "2015-07-14 20:10:33",
            "modified": "2015-07-24 09:25:22",
            "deleted": "0",
            "deleted_date": "0000-00-00 00:00:00"
        }
    }, {
        "id": "110268",
        "multiplier": null,
        "quantity": "1.00",
        "ingredient_id": "493",
        "ingredient_unit_id": "31",
        "recipe_id": "82",
        "Ingredient": {
            "id": "493",
            "name": "honey",
            "plural": "honey",
            "IngredientGroup": [{
                "id": "87",
                "name": "Non-refined\/ Natural sugar",
                "IngredientGroupsIngredient": {
                    "id": "630",
                    "ingredient_group_id": "87",
                    "ingredient_id": "493",
                    "created": "0000-00-00 00:00:00",
                    "modified": "0000-00-00 00:00:00",
                    "deleted": "0",
                    "deleted_date": "0000-00-00 00:00:00"
                }
            }, .......

TypeReferencing只能使用原始变量,我必须使用String []作为我的第一个参数而不是String。 我该怎么办?

谢谢!

0 个答案:

没有答案