简体   繁体   English

如何在java中读取json的数组值

[英]How to read a array value form json in java

I have following json data:- 我有以下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
}

Method to assert json:- 断言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);

}

When I try to get json value from by using the following, Its works well. 当我尝试通过使用以下来获取json值时,它运行良好。

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

I want to get array value from json such as details of 1st book. 我想从第一本书的细节中获取json的数组值。

I have tried the following, that doesn't help me out. 我试过以下,这对我没有帮助。

json paths are as follows:- json路径如下: -

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

How Can I do this ? 我怎样才能做到这一点 ?

According to this answer , you should be able to get the mentioned properties like this: 根据这个答案 ,你应该能够得到这样的提到的属性:

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

Edit: 编辑:

Which version of jackson and beanutils are you using? 您使用的是哪个版本的jackson和beanutils? I've modified your method a bit and made a simple test case, using beanutils 1.9.2 and jackson 2.6.5 tests seem to pass: 我已经修改了你的方法并做了一个简单的测试用例,使用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;
    }
}

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

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