如何将JSON对象转换为java jsonReader

时间:2016-03-21 14:50:15

标签: java json

我有一个JSON如下: -

{
    "products":
    {
        "productsApp15": {
            "code": "productsApp16",
            "name": "productsApp16",
            "attribute_set": "Apparel",
            "product_type": "product",
            "status": "active"
            }
    }
}

现在我需要一个可以自动转换它的功能: -

 final JsonReader jsonReader = Json.createReader(new StringReader("{\n" +
                "    \"products\":\n" +
                "    {\n" +
                "        \"productsApp13\": {\n" +
                "            \"code\": \"productsApp13\",\n" +
                "            \"name\": \"productsApp13\",\n" +
                "            \"attribute_set\": \"Apparel\",\n" +
                "            \"product_type\": \"product\",\n" +
                "            \"status\": \"active\"\n" +
                "            }\n" +
                "    }\n" +
                "}"));

为此我尝试用/ n追加/连接字符串,但它被当作新行。我知道它是正确的,但有任何方法可以自动获得该输出。

我尝试过类似下面的内容: -

        String sCurrentLine;
        StringBuilder sb = new StringBuilder("");
        br = new BufferedReader(new FileReader("./src/test/com/testdata/HTTPHelperTest.csv"));

    while ((sCurrentLine = br.readLine()) != null) {
        sb.append(sCurrentLine);
        sb.append("\n");
    }
    br.close();
    System.out.println("Value Json"+sb);

任何解决方案都是可行的。

1 个答案:

答案 0 :(得分:1)

您需要为\

添加转义字符\n
 while ((sCurrentLine = br.readLine()) != null) {
        sb.append(sCurrentLine);
        sb.append("\\n");
    }