简体   繁体   English

如何将JSON对象转换为Java jsonReader

[英]How to convert JSON object into java jsonReader

I have a JSON as below:- 我有一个JSON,如下所示:

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

Now I need a function which can convert it like below automatically:- 现在我需要一个可以像下面这样自动转换的函数:-

 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" +
                "}"));

For that I tried to append/concatenate the string with /n but it was taken up as new line. 为此,我尝试使用/ n附加/连接字符串,但将其作为新行使用。 I know it is right to but is there any way by which I can get that output automatically. 我知道这样做是正确的,但是有什么方法可以自动获取该输出。

I tried something like below:- 我尝试了以下内容:

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

Any solution is appreicable. 任何解决方案都是有意义的。

You need to add an escape character \\ for \\n 您需要为\\n添加转义字符\\

 while ((sCurrentLine = br.readLine()) != null) {
        sb.append(sCurrentLine);
        sb.append("\\n");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM