1

我有以下 json 数据:-

{
    "store": {
        "book": [
            {
                "category": "reference",
                "author": "Nigel Rees",
                "title": "Sayings of the Century",
                "price": 8.95
            },
            {
                "category": "fiction",
                "author": "Evelyn Waugh",
                "title": "Sword of Honour",
                "price": 12.99
            },
            {
                "category": "fiction",
                "author": "Herman Melville",
                "title": "Moby Dick",
                "isbn": "0-553-21311-3",
                "price": 8.99
            },
            {
                "category": "fiction",
                "author": "J. R. R. Tolkien",
                "title": "The Lord of the Rings",
                "isbn": "0-395-19395-8",
                "price": 22.99
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    },
    "expensive": 10
}

断言 json 的方法:-

public void assertJsonvalue (String description, String jsonString,
            String path, Object expectedValue) {

        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> jsonMap = null;
        try {
            jsonMap = objectMapper.readValue(jsonString,
                    new TypeReference<Map<String, Object>>() {
                    });

        } catch (IOException e) {
            fail("Could not parse json from string:" + jsonString);
        }

        Object actualValue = null;
        try {
            actualValue = PropertyUtils.getProperty(jsonMap, path);
            System.out.println("actualValue" + actualValue);
        } catch (IllegalAccessException e) { 
          // error here
        }
        assertEquals(description, expectedValue, actualValue);

}

当我尝试使用以下方法获取 json 值时,它运行良好。

    assertJsonValue("bicycle color", json, "store.bicycle.color", "red");

我想从 json 中获取数组值,例如第一本书的详细信息。

我尝试了以下方法,但这对我没有帮助。

json路径如下:-

  1. “store.book[0]”
  2. “store.book.[0]”
  3. “store.book.category”

我怎样才能做到这一点 ?

4

1 回答 1

3

根据这个答案,您应该能够像这样获得提到的属性:

assertJsonValue("...", json, "store.(book)[0].category", "reference");

编辑:

您使用的是哪个版本的 jackson 和 beanutils?我稍微修改了你的方法并做了一个简单的测试用例,使用beanutils 1.9.2jackson 2.6.5测试似乎通过了:

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.beanutils.PropertyUtils;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.*;

public class TestJson {

    private static final String JSON = "{\n" +
            "    \"store\": {\n" +
            "        \"book\": [\n" +
            "            {\n" +
            "                \"category\": \"reference\",\n" +
            "                \"author\": \"Nigel Rees\",\n" +
            "                \"title\": \"Sayings of the Century\",\n" +
            "                \"price\": 8.95\n" +
            "            }\n" +
            "        ],\n" +
            "        \"bicycle\": {\n" +
            "            \"color\": \"red\",\n" +
            "            \"price\": 19.95\n" +
            "        }\n" +
            "    },\n" +
            "    \"expensive\": 10\n" +
            "}";

    @Test
    public void testJson() {
        assertTrue(assertJsonValue(JSON, "store.(book)[0].category", "reference"));
        assertTrue(assertJsonValue(JSON, "store.(book)[0].author", "Nigel Rees"));
        assertTrue(assertJsonValue(JSON, "store.(book)[0].title", "Sayings of the Century"));
        assertTrue(assertJsonValue(JSON, "store.(book)[0].price", 8.95));
    }

    public boolean assertJsonValue(String jsonString,
                                   String path,
                                   Object expectedValue) {

        ObjectMapper objectMapper = new ObjectMapper();
        try {
            Object actual = PropertyUtils
                    .getProperty(objectMapper.readValue(jsonString, Object.class), path);

            if (actual.equals(expectedValue)) {
                return true;
            }

        } catch (IOException | ReflectiveOperationException e) {
            // handle error
        }
        return false;
    }
}
于 2016-04-25T07:23:08.193 回答