【问题标题】:Nested JSON Array in JavaJava中的嵌套JSON数组
【发布时间】:2017-01-15 08:12:12
【问题描述】:

我需要创建一个如下所示的 json 响应。我尝试了一些代码,但无法获得我需要的东西。在 java 代码中需要帮助来创建嵌套数组以根据类别以及下面 json 中的类别详细信息对食物进行分组

{
"menu": {
"items": [{
        "id": 1,
        "code": "hot1_sub1_mnu",
        "name": "Mutton",
        "status": "1",
        "sub_items": [{
            "id": "01",
            "name": "Mutton Pepper Fry",
            "price": "100"
        }, {
            "id": "02",
            "name": "Mutton Curry",
            "price": "100"
        }]
    },
    {
        "id": "2",
        "code": "hot1_sub2_mnu",
        "name": "Sea Food",
        "status": "1",
        "sub_items": [{
            "id": "01",
            "name": "Fish Fry",
            "price": "150"
        }]
    },
    {
        "id": "3",
        "code": "hot1_sub3_mnu",
        "name": "Noodles",
        "status": "1",
        "sub_items": [{
            "id": "01",
            "name": "Chicken Noodles",
            "price": "70"
        }, {
            "id": "02",
            "name": "Egg Noodles",
            "price": "60"
        }]
    }
]
}
}

到目前为止,我尝试过的将在一个数组中给出响应。

@Path("/items")
public class HotelsMenu {
@GET
@Path("/getitems")
@Produces(MediaType.APPLICATION_JSON)
public String doLogin(@QueryParam("hotelcode") String hotelcode) {

JSONObject response = new JSONObject();
JSONArray hotelDetails = checkCredentials(hotelcode);
try {
    response.put("hotels", hotelDetails);
    response.put("status", (hotelDetails == new JSONArray()) ? "false" : "true");
} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
return response.toString();
}

private JSONArray checkCredentials(String hotelcode) {
System.out.println("Inside checkCredentials");

JSONArray result = new JSONArray();

    try {
        result = DBConnection.checkItems(hotelcode);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

return result;
}

public static JSONArray checkItems(String hotelcode) throws Exception {
int id;
String code = hotelcode + "_mnu";
String name = null;
String name1 = null;
String status;
String price;

Connection dbConn = null;
Connection dbConn1 = null;
JSONArray hotels = new JSONArray();
JSONArray menu = new JSONArray();
try {
    try {
        dbConn = DBConnection.createConnection();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Statement stmt = dbConn.createStatement();
    String query = "SELECT * FROM " + code + " where Status='1'";
    System.out.println(query);
    ResultSet rs1 = stmt.executeQuery(query);
    System.out.println("hai");

    while (rs1.next()) {

        JSONObject hotel = new JSONObject();
        id = rs1.getInt("Id");
        hotel.put("id", id);
        code = rs1.getString("Code");
        System.out.println(code);
        hotel.put("code", code);
        name = rs1.getString("Name");
        hotel.put("name", name);
        status = rs1.getString("Status");
        hotel.put("status", status);
        hotels.put(hotel);
        System.out.println("Hotel1:" + hotels);
        try {
            dbConn1 = DBConnection.createConnection();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Statement stmt1 = dbConn1.createStatement();
        String query1 = "SELECT * FROM " + code + "  where Status='1' ";
        System.out.println(query1);
        ResultSet rs2 = stmt1.executeQuery(query1);

        while (rs2.next()) {

            JSONArray hotel1 = new JSONArray();
            JSONObject hotelmenu = new JSONObject();
            id = rs2.getInt("Id");
            hotelmenu.put("id", id);
            name1 = rs2.getString("Name");
            hotelmenu.put("name", name1);
            price = rs2.getString("Price");
            hotelmenu.put("price", price);
            hotel1.put(hotelmenu);

            hotels.put(hotel1);

            System.out.println(hotels);
        }

    }
} catch (SQLException sqle) {
    throw sqle;
} catch (Exception e) {
    // TODO Auto-generated catch block
    if (dbConn != null) {
        dbConn.close();
    }
    throw e;
} finally {
    if (dbConn != null) {
        dbConn.close();
    }
}
return hotels;
}
}

【问题讨论】:

  • 您想要的 JSON 结构是无效的 JSON。请发布有效的 JSON,否则很难理解您真正想要什么。我的建议:不要在同一个结构中混合数组和对象。设计您的对象模型,并将其映射到 JSON。在 Java 中,您不会使用包含混合类型值的 List。对 JSON 做同样的事情。

标签: java arrays json


【解决方案1】:

正如之前的答案所暗示的,您应该重新设计您的模型。我只是对它进行了快速重组。检查这是否符合您的目的 -

{
"menu": {
    "items": [{
            "id": 1,
            "code": "hot1_sub1_mnu",
            "name": "Mutton",
            "status": "1",
            "sub_items": [{
                "id": "01",
                "name": "Mutton Pepper Fry",
                "price": "100"
            }, {
                "id": "02",
                "name": "Mutton Curry",
                "price": "100"
            }]
        },
        {
            "id": "2",
            "code": "hot1_sub2_mnu",
            "name": "Sea Food",
            "status": "1",
            "sub_items": [{
                "id": "01",
                "name": "Fish Fry",
                "price": "150"
            }]
        },
        {
            "id": "3",
            "code": "hot1_sub3_mnu",
            "name": "Noodles",
            "status": "1",
            "sub_items": [{
                "id": "01",
                "name": "Chicken Noodles",
                "price": "70"
            }, {
                "id": "02",
                "name": "Egg Noodles",
                "price": "60"
            }]
        }
    ]
}}

如果结构没问题,如果需要帮助生成上述 Json 的 Java 代码,请告知。

还可以通过以下库 - Jackson tutorialGson tutorial

【讨论】:

  • 这是我需要的确切格式。请帮助java代码
【解决方案2】:

//导入 java.util.ArrayList; //导入org.bson.Document;

   Document root = new Document();
    Document rootMenu = new Document();
    ArrayList rootMenuItems = new ArrayList();
    Document rootMenuItems0 = new Document();
    ArrayList rootMenuItems0Sub_items = new ArrayList();
    Document rootMenuItems0Sub_items0 = new Document();
    Document rootMenuItems0Sub_items1 = new Document();
    Document rootMenuItems1 = new Document();
    ArrayList rootMenuItems1Sub_items = new ArrayList();
    Document rootMenuItems1Sub_items0 = new Document();
    Document rootMenuItems2 = new Document();
    ArrayList rootMenuItems2Sub_items = new ArrayList();
    Document rootMenuItems2Sub_items0 = new Document();
    Document rootMenuItems2Sub_items1 = new Document();

    rootMenuItems0.append("id", 1);

    rootMenuItems0.append("code", "hot1_sub1_mnu");

    rootMenuItems0.append("name", "Mutton");

    rootMenuItems0.append("status", "1");

    rootMenuItems0Sub_items0.append("id", "01");

    rootMenuItems0Sub_items0.append("name", "Mutton Pepper Fry");

    rootMenuItems0Sub_items0.append("price", "100");

    rootMenuItems0Sub_items1.append("id", "02");

    rootMenuItems0Sub_items1.append("name", "Mutton Curry");

    rootMenuItems0Sub_items1.append("price", "100");

    rootMenuItems1.append("id", "2");

    rootMenuItems1.append("code", "hot1_sub2_mnu");

    rootMenuItems1.append("name", "Sea Food");

    rootMenuItems1.append("status", "1");

    rootMenuItems1Sub_items0.append("id", "01");

    rootMenuItems1Sub_items0.append("name", "Fish Fry");

    rootMenuItems1Sub_items0.append("price", "150");

    rootMenuItems2.append("id", "3");

    rootMenuItems2.append("code", "hot1_sub3_mnu");

    rootMenuItems2.append("name", "Noodles");

    rootMenuItems2.append("status", "1");

    rootMenuItems2Sub_items0.append("id", "01");

    rootMenuItems2Sub_items0.append("name", "Chicken Noodles");

    rootMenuItems2Sub_items0.append("price", "70");

    rootMenuItems2Sub_items1.append("id", "02");

    rootMenuItems2Sub_items1.append("name", "Egg Noodles");

    rootMenuItems2Sub_items1.append("price", "60");

    if (!rootMenuItems.isEmpty()) {
        rootMenu.append("items", rootMenuItems);
    }
    if (!rootMenuItems0Sub_items.isEmpty()) {
        rootMenuItems0.append("sub_items", rootMenuItems0Sub_items);
    }
    if (!rootMenuItems0Sub_items0.isEmpty()) {
        rootMenuItems0Sub_items.add(rootMenuItems0Sub_items0);
    }
    if (!rootMenuItems0Sub_items.isEmpty()) {
        rootMenuItems0.append("sub_items", rootMenuItems0Sub_items);
    }
    if (!rootMenuItems0Sub_items1.isEmpty()) {
        rootMenuItems0Sub_items.add(rootMenuItems0Sub_items1);
    }
    if (!rootMenuItems0Sub_items.isEmpty()) {
        rootMenuItems0.append("sub_items", rootMenuItems0Sub_items);
    }
    if (!rootMenuItems0.isEmpty()) {
        rootMenuItems.add(rootMenuItems0);
    }
    if (!rootMenuItems.isEmpty()) {
        rootMenu.append("items", rootMenuItems);
    }
    if (!rootMenuItems1Sub_items.isEmpty()) {
        rootMenuItems1.append("sub_items", rootMenuItems1Sub_items);
    }
    if (!rootMenuItems1Sub_items0.isEmpty()) {
        rootMenuItems1Sub_items.add(rootMenuItems1Sub_items0);
    }
    if (!rootMenuItems1Sub_items.isEmpty()) {
        rootMenuItems1.append("sub_items", rootMenuItems1Sub_items);
    }
    if (!rootMenuItems1.isEmpty()) {
        rootMenuItems.add(rootMenuItems1);
    }
    if (!rootMenuItems.isEmpty()) {
        rootMenu.append("items", rootMenuItems);
    }
    if (!rootMenuItems2Sub_items.isEmpty()) {
        rootMenuItems2.append("sub_items", rootMenuItems2Sub_items);
    }
    if (!rootMenuItems2Sub_items0.isEmpty()) {
        rootMenuItems2Sub_items.add(rootMenuItems2Sub_items0);
    }
    if (!rootMenuItems2Sub_items.isEmpty()) {
        rootMenuItems2.append("sub_items", rootMenuItems2Sub_items);
    }
    if (!rootMenuItems2Sub_items1.isEmpty()) {
        rootMenuItems2Sub_items.add(rootMenuItems2Sub_items1);
    }
    if (!rootMenuItems2Sub_items.isEmpty()) {
        rootMenuItems2.append("sub_items", rootMenuItems2Sub_items);
    }
    if (!rootMenuItems2.isEmpty()) {
        rootMenuItems.add(rootMenuItems2);
    }
    if (!rootMenuItems.isEmpty()) {
        rootMenu.append("items", rootMenuItems);
    }
    if (!rootMenu.isEmpty()) {
        root.append("menu", rootMenu);
    }

    System.out.println(root.toJson());

【讨论】:

    猜你喜欢
    • 2013-07-28
    • 1970-01-01
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多