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

Posted

技术标签:

【中文标题】如何在java中读取json格式的数组值【英文标题】:How to read a array value form json in java 【发布时间】:2016-04-25 07:02:15 【问题描述】:

我有以下 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路径如下:-

    “store.book[0]” “store.book.[0]” “store.book.category”

我该怎么做?

【问题讨论】:

你用的是什么库 我正在使用以下库,例如 com.fasterxml.jackson.databind.JsonNode 和 org.codehaus.jackson.JsonNode 【参考方案1】:

根据this answer,您应该能够像这样获得提到的属性:

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;
    

【讨论】:

以下对我不起作用。路径(“store.(book)[0].category”)返回“null”。 也许您使用的是旧版本的 jackson 或 beanutils?我已经更新了我的答案,以展示我是如何测试的。 我已将 commons-beanutils 版本更新为 1.9.2。现在它运作良好。谢谢

以上是关于如何在java中读取json格式的数组值的主要内容,如果未能解决你的问题,请参考以下文章

如何读取json字符串中的数组

java怎么读取json格式的数据

java怎么读取json格式的数据

如何在JSON.NET中读取json对象值中的long值数组

cookie可以存一个json吗或者存一个数组

java怎么读取json格式的数据