Mustache-Java:如何通过键在Map中获得价值 [英] Mustache-Java: How to get value in Map by key

查看:330
本文介绍了Mustache-Java:如何通过键在Map中获得价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java中使用JMustache.

I am using JMustache in Java.

我的Mustache模板中有Map变量.像Java(map.get())一样,如何获取地图中特定键的值.

I have Map variable in my Mustache template. How can I fetch a value at a particular key in the map, as we do in Java (map.get()).

我知道如何遍历模板中Map的键和值.但是我想要一个没有迭代的解决方案,以便我可以评估一个表达式,例如:

I know how to iterate through keys and values of a Map in a template. But I want a solution without iteration so that I can evaluate an expression such as:

{
    "cities": [
        {
            "Tokyo": {
                "overview": {
                    "population": "19000000",
                    "area": "450000"
                }
            }
        },
        {
            "Sydney": {
                "overview": {
                    "population": "4500000",
                    "area": "6250000"
                }
            }
        }
    ]
}

模板

"The population of Tokyo is: {{cities['Tokyo'].overview.population}}"

推荐答案

首先,您的小胡子语法有问题. cities['Tokyo']无效. cities是一个数组.您可以遍历数组,但不能根据其键值(或与此相关的任何其他条件)选择元素.

First, there is an issue with your mustache syntax. cities['Tokyo'] is not valid. cities is an array. You can iterate over an array, but not select an element based on its key value (or any other condition for that matter).

因此,此JSON对象更合适:

So this JSON object fits better:

{
    "cities": {
        "Tokyo": {
            "overview": {
                "population": "19000000",
                "area": "450000"
            }
        },
        "Sydney": {
            "overview": {
                "population": "4500000",
                "area": "6250000"
            }
        }
    }
}

第二,您必须使用MapList对象将JSON String解析为结构.大多数JSON解析器都可以做到这一点.在此示例中,我使用 noggit .

Second, you have to parse the JSON String to a structure using Map and List objects. Most JSON parsers are able to do this. In this example, I use noggit.

@Test
public void testSO_45787572() throws IOException {
    final String json = 
        "{\n" +
        "   \"cities\": {\n" +
        "       \"Tokyo\": {\n" +
        "           \"overview\": {\n" +
        "               \"population\": \"19000000\",\n" +
        "               \"area\": \"450000\"\n" +
        "           }\n" +
        "       },\n" +
        "       \"Sydney\": {\n" +
        "           \"overview\": {\n" +
        "               \"population\": \"4500000\",\n" +
        "               \"area\": \"6250000\"\n" +
        "           }\n" +
        "       }\n" +
        "   }\n" +
        "}";
    assertEquals("The population of Tokyo is: 19000000", Mustache.compiler()
        .compile(new StringReader("The population of Tokyo is: {{cities.Tokyo.overview.population}}"))
        .execute(new ObjectBuilder(new JSONParser(json)).getObject())
    );
}

这篇关于Mustache-Java:如何通过键在Map中获得价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆