0

I have a JSON as below:-

{
    "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. 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.

1 Answer 1

1

You need to add an escape character \ for \n

 while ((sCurrentLine = br.readLine()) != null) {
        sb.append(sCurrentLine);
        sb.append("\\n");
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks .. Your answer have provide me lil solution of one part .. vote up for same.. but it is still not making string like above .. I also sb.append("+");
Well, "+" exists in your second code sample exists since the string has been parted to separate lines for better visibility. You can use tabs \t and new line \n to make it look like the first code sample in your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.