0

I am using google api. The expected response is

{
  "responses": [
    {
      "textAnnotations": [
        {
          "locale": "en",
          "description": "ABBEY\nROAD NW8\nCITY OF WESTMINSTER\n",
          "boundingPoly": {
            "vertices": [
              {
                "x": 45,
                "y": 43
              },
              {
                "x": 269,
                "y": 43
              },
              {
                "x": 269,
                "y": 178
              },
              {
                "x": 45,
                "y": 178
              }
            ]
          }
        },

I am getting this type of response.

 {
  "responses": [
    {
      "textAnnotations": [
        {
          "locale": "en",
          "description": "ABBEY 
                          ROAD NW8 
                          CITY OF WESTMINSTER",
          "boundingPoly": {
            "vertices": [
              {
                "x": 45,
                "y": 43
              },
              {
                "x": 269,
                "y": 43
              },
              {
                "x": 269,
                "y": 178
              },
              {
                "x": 45,
                "y": 178
              }
            ]
          }
        },

Apex removes \n from the response. I tried

HttpResponse response = http.send(request); 

String strResponse = response.getBody();
strResponse.replaceAll('\n', '<br/>'); 

But it is not working Result shows in the same line

5
  • Are you displaying the response in `outputText' in Visualforce? Commented Jan 10, 2018 at 11:42
  • Yes I am displaying it in output text. I tried escape false but didnt work Commented Jan 10, 2018 at 11:48
  • 1
    Tried this? <apex:outputLabel value="{!SUBSTITUTE(JSENCODE(strResponse ), '\\n', '<br/>')}" escape="false"/ > Commented Jan 10, 2018 at 11:53
  • awesome its working Commented Jan 10, 2018 at 11:57
  • 1
    @HemantJain Could be added as Answer so other's can use it .
    – NITHESH K
    Commented Jan 10, 2018 at 12:04

1 Answer 1

2

Try displaying the String with the help of JSENCODE and then replacing \n with <br/> tag.

Below is the sample code to display the string on VF page:

<apex:outputLabel value="{!SUBSTITUTE(JSENCODE(strResponse ), '\\n', '<br/>')}" escape="false"/ >

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .