0

我只是想问一下,如何简单地解析 Java 中的嵌套 JSON 结构,如下所示:

{
   "goods":{
      "NY Store":[
         {
            "product":"Teddybear",
            "available":"20"
         },
         {
            "product":"Mountain bike",
            "available":"0"
         }]
      "LA Store":[
         {
            "product": ....and so on

我想从所有商店获取所有产品,但列表很大(200 家商店)。请问有什么提示和提示吗?

4

4 回答 4

2

这是您在 Jackson 库中的特定用例的快速介绍。

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonSerialize;

public class JSONTest {
    /**
     * @param args
     * @throws IOException
     * @throws JsonMappingException
     * @throws JsonGenerationException
     */
    public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
        HashMap<String, ArrayList<InventoryItem>> fs = new HashMap<String, ArrayList<InventoryItem>>();
        for (int i = 0; i < 5; i++) {
            ArrayList<InventoryItem> its = new ArrayList<InventoryItem>();
            its.add(new InventoryItem("teddy", 20));
            its.add(new InventoryItem("diny", 10));
            fs.put(Long.toString(System.nanoTime()), its);
        }
        StoreContianer g = new StoreContianer(fs);
        ObjectMapper objm = new ObjectMapper();
        StringWriter sw = new StringWriter();
        objm.writeValue(sw, g);
        System.out.println(sw.toString());
        StoreContianer c = objm.readValue(
                "{\"goods\":{\"55278650620460\":[{\"product\":\"teddy\",\"available\":20},{\"product\":\"diny\",\"available\":10}],\"55278650631327\":[{\"product\":\"teddy\",\"available\":20},{\"product\":\"diny\",\"available\":10}],\"55278650628131\":[{\"product\":\"teddy\",\"available\":20},{\"product\":\"diny\",\"available\":10}],\"55278650582748\":[{\"product\":\"teddy\",\"available\":20},{\"product\":\"diny\",\"available\":10}],\"55278650624615\":[{\"product\":\"teddy\",\"available\":20},{\"product\":\"diny\",\"available\":10}]}}",
                StoreContianer.class);
        StringWriter sw2 = new StringWriter();
        objm.writeValue(sw2, c);
        System.out.println(sw2.toString());
    }
}

@JsonSerialize
class StoreContianer {
    private final HashMap<String, ArrayList<InventoryItem>> goods;

    public StoreContianer(@JsonProperty("goods") HashMap<String, ArrayList<InventoryItem>> goods) {
        this.goods = goods;
    }

    public HashMap<String, ArrayList<InventoryItem>> getGoods() {
        return goods;
    }
}

@JsonSerialize
class InventoryItem {
    private final String product;
    private final int available;

    public InventoryItem(@JsonProperty("product") String product, @JsonProperty("available") int available) {
        this.product = product;
        this.available = available;
    }

    public String getProduct() {
        return product;
    }

    public int getAvailable() {
        return available;
    }
}
于 2012-05-05T01:14:23.243 回答
1

为什么不使用图书馆?http://www.json.org/java/

具体来说,

public JSONObject(String source) throws JSONException {
        this(new JSONTokener(source));
}
于 2012-05-05T00:48:58.527 回答
0

我发现这个google 库对于解析 JSON 中的嵌套对象非常强大。

List<String> products = JsonPath.read(json, "$.goods.NY Store[*].product");

这将为您返回每个“纽约商店”列表中所有产品的列表。

于 2014-09-27T00:31:53.490 回答
0
    //import java.util.ArrayList;
    //import org.bson.Document;

    Document root = Document.parse("{\n"
            + "   \"goods\":{\n"
            + "      \"NY Store\":[\n"
            + "         {\n"
            + "            \"product\":\"Teddybear\",\n"
            + "            \"available\":\"20\"\n"
            + "         },\n"
            + "         {\n"
            + "            \"product\":\"Mountain bike\",\n"
            + "            \"available\":\"0\"\n"
            + "         }]}}");

    System.out.println(((String) ((Document) ((ArrayList) ((Document) root.get("goods")).get("NY Store")).get(0)).get("product")));
    System.out.println(((String) ((Document) ((ArrayList) ((Document) root.get("goods")).get("NY Store")).get(0)).get("available")));
    System.out.println(((String) ((Document) ((ArrayList) ((Document) root.get("goods")).get("NY Store")).get(1)).get("product")));
    System.out.println(((String) ((Document) ((ArrayList) ((Document) root.get("goods")).get("NY Store")).get(1)).get("available")));
于 2019-10-02T21:26:19.877 回答