JSONObject和JDBC返回格式json不正确

时间:2016-10-04 19:16:38

标签: java json database jdbc

我的小项目遇到了问题。我花了大约4天的时间,还没有最好的解决方案。

这是我的源代码:

public static JSONObject fetchPersonCarInfo() throws Exception {

    // get data from 2 tables: Cars and Manufacturer            
    String query = "SELECT * from mytable";       
        statement = connection.prepareStatement(query);         
        rs = statement.executeQuery();
        JSONArray carsArray = null;                         
        while(rs.next()) {

            carObj.put("id", (new  string(rs.getString("id"))));                    


            carsArray.put(carObj);

            for(int i=0 ; i< carsArray.length() ; i++) {

            manufacturerObj.put(new String(rs.getString("manufacturer_id")), carsArray);
            manufacturerObj.put(new String(rs.getString("manufacturer_name")), carsArray);
            }
            System.out.println(manufacturerObj.toString());
    }
            System.out.println("Ok");
    }
    catch (Exception e) {
        // TODO Auto-generated catch block
        if (connection != null) {
            connection.close();
        }
            throw e;
    } 
    finally {
            if (connection != null) {
                connection.close();
        }
    }

    return manufacturerObj; 
}        

然后我构建我的代码并通过url进行部署,它看起来不正确,并且出现了我预期的错误,这是我的结果json格式:

{
    "1":
    [
        {
            "manufacturer_id": "1",
            "manufacturer_name": "Honda"
        },
        {
            "id": "4",
            "name": "Honda1",
            "price": "66000"
        }
    ],        
}

我如何更改我的代码以构建正确的上述json格式。非常感谢...

1 个答案:

答案 0 :(得分:0)

carObj部分很好,所以我建议将它作为一种方法。

public static JSONObject getCarJSON(String id, String name, String price) {
    JSONObject carObj = new JSONObject();                   
    carObj.put("id", id);                    
    carObj.put("name", name);
    carObj.put("price", price);
    return carObj;
}

你的问题似乎就在这里。您正在向personCarObj添加carsArray。这应该只是包含汽车。

JSONObject personCarObj = new JSONObject();                         
personCarObj.put("manufacturer_id", (new String(rs.getString("manufacturer_id"))));
personCarObj.put("manufacturer_name", (new String(rs.getString("manufacturer_name"))));
carsArray.put(personCarObj); 

在这里。对于每一辆&#34;汽车&#34;在carsArray中,您将键值对放入manufacturerObj,其中值始终为carsArraymanufacturerObj可能包含制造商信息。

for(int i=0 ; i< carsArray.length() ; i++) {

    manufacturerObj.put(new String(rs.getString("manufacturer_id")), carsArray);
    manufacturerObj.put(new String(rs.getString("manufacturer_name")), carsArray);
}

因此,根据这些建议,您希望尝试获得类似于最初显示的非JSON的内容。

{
    "manufacturers": [{
        "id": "1",
        "name": "Honda",
        "cars": [{
            "id": "4",
            "name": "Honda1",
            "price": "66000"
        }]
    }, {
        "id": "2",
        "name": "Toyota",
        "cars": [{
            "id": "551",
            "name": "Toyota51",
            "price": "3233333"
        }]
    }]
}

以下代码可能会关闭(警告:未经测试)。地图的原因是当您已经看到制造商时捕获,然后附加数据而不是重复。

JSONArray manufacturerArray = new JSONArray();
HashMap<String, JSONArray> manufacturerMap = new HashMap<String, JSONArray>();

while(rs.next()) {
    String mId = rs.getString("manufacturer_id");
    JSONArray carsArray = manufacturerMap.get(mId);
    if (carsArray == null) {
        carsArray = new JSONArray();
        manufacturerMap.put(mId, carsArray);
    }
    JSONObject manufacturer = new JSONObject();                         
    manufacturer.put("id", mId);
    manufacturer.put("name", rs.getString("manufacturer_name"));                  

    String carId = rs.getString("id");                    
    String carName = rs.getString("name");
    String carPrice = rs.getString("price");
    carsArray.put(getCarJSON(carId, carName, carPrice));

    manufacturer.put("cars", carsArray);
    manufacturerArray.put(manufacturer);
}
JSONObject result = new JSONObject();
result.put("manufacturers", manufacturerArray);

可以使用Jackson / Gson制作一些像这样的POJO

class Manufacturer {
    String id, name;
    List<Car> cars;
} 

class Car {
    String id, name, price;
}

然后你会序列化List<Manufacturer>