簡體   English   中英

如何在java中讀取json的數組值

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

我有以下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”

我怎樣才能做到這一點 ?

根據這個答案 ,你應該能夠得到這樣的提到的屬性:

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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM