We help IT Professionals succeed at work.

json string error

royjayd
royjayd asked
on
hi guys

this is my json string


"TRADE_LAYOUT":{
      "fields":{
         "10021/INOPS-RAF +MONEY/INDIA":{          
            "id":"1160GB0421/UKOPS-FX INTERBANK PROC+INVEST/EMEA",          
            "order":7,
            "caption":"10021                          
            INOPS-RAF +MONEY                          
            INDIA                          
            ",
           
          "fields":"[object Object],[object Object]"
         }

when i validate the json string in Json formatter ,it shows error in the
"caption". The error is >>Invalid characters found

Any idea how i can fix it?

thanks
Comment
Watch Question

Commented:
Replace newline in caption with \n.

"TRADE_LAYOUT":{
      "fields":{
         "10021/INOPS-RAF +MONEY/INDIA":{           
            "id":"1160GB0421/UKOPS-FX INTERBANK PROC+INVEST/EMEA",           
            "order":7,
            "caption":"10021\nINOPS-RAF +MONEY\nINDIA",            
          "fields":"[object Object],[object Object]"
         }

Open in new window

Author

Commented:
but i am already doing '\n' on my java side and passing the json string to UI
like this
My java code

JSONObject myson = new JSONObject();
myson.put(CAPTION, returnFormatedName(name));      
...

public String returnFormatedName(String name){
            String code = "";
            String aligner = "                          ";
            StringTokenizer st = new StringTokenizer(name,"/");
            StringBuffer codeText = new StringBuffer();      
            while (st.hasMoreTokens()) {                  
                  code = st.nextToken()+aligner;
                  codeText .append(code).append("\n");                   
           }
            return codeText .toString(); // caption would contain \n
      }

and that is why i a am getting the caption in seperate line like this
"caption":"10021                          
            INOPS-RAF +MONEY                          
            INDIA                          
            ",      

but when i validate my json it fails. any idea what i am missing?

thanks

Commented:
In your java side, you should put \\n instead of \n.

Author

Commented:
Ok will try that.. Can u plz tell me what is the difference between \\n and Single slash n
Thanks

Commented:
\n is an escaped newline.
\\n is an escaped of above one, escaped of escaped newline.

You need the escaped newline in your Java to print a new line, but you need the latter to print the escaped newline.

Author

Commented:
thanks

but when i use
 while (st.hasMoreTokens()) {                  
                  code = st.nextToken();
                  codeText .append(code).append("\\n");            
     }

its displaying like this in the UI
10021\nINOPS-RAF +MONEY\nINDIA  but i want the next line like this

10021
INOPS-RAF +MONEY
INDIA




but in the UI its not displaying in next line. It displays as
                   

Author

Commented:
any idea what i am doing wrong
Commented:
Now json string is working. To display new line in HTML, you have to replace \n by <br>.

This can be easily done by...

caption.replace("\n","br");

or

If you have no use of \n in the caption, use this code in your java side.

codeText .append(code).append("<br>"); instead of codeText .append(code).append("\\n");

Author

Commented:
actually i am using Flex on the UI ( sorry forgot to say that)
so <br> isnt working correctly, i am seeing
10021<br>INOPS-RAF +MONEY<br>INDIA  

i know '\n' works fine with flex. any idea how i can make it work with flex

thanks

Commented:
If use \\n, what does it look like?

Author

Commented:
if i use \\n
it looks like

10021\nINOPS-RAF +MONEY\nINDIA  
Top Expert 2011
Commented:
By StingRaY recommendation you can use <br> in Flex as well but you have to use htmlText instead text

Example:

var str:String = "Hello<br>World";

label.text = str;
label.htmlText = str;

Will display differently

Author

Commented:
any idea how i can make it work with 'text' only ?
I have the freedom to change the java code only..cant change the flex side

thanks
Top Expert 2016
Commented:
>>
but when i use
 while (st.hasMoreTokens()) {                  
                  code = st.nextToken();
                  codeText .append(code).append("\\n");            
     }

its displaying like this in the UI
10021\nINOPS-RAF +MONEY\nINDIA  but i want the next line like this
>>

well then, try
while (st.hasMoreTokens()) {                  
                  code = st.nextToken();
                  codeText .append(code).append("\n");            
     }

OR

final String SEP = System.getProperty("line.separator");

 while (st.hasMoreTokens()) {                  
                  code = st.nextToken();
                  codeText .append(code).append(SEP);            
     }

Open in new window


Three simple back-slashes, as below, should do the trick:

"TRADE_LAYOUT":{
  "fields":{
    "10021/INOPS-RAF +MONEY/INDIA":{           
      "id":"1160GB0421/UKOPS-FX INTERBANK PROC+INVEST/EMEA",           
      "order":7,
      "caption":"10021                          \
      INOPS-RAF +MONEY                          \
      INDIA                          \
      ",            
      "fields":"[object Object],[object Object]"
    }
  }
}

Open in new window

Author

Commented:
I am actually using that json data to disply on a flex UI..so whats happening is the slashes are getting displayed as is.
Top Expert 2016

Commented:
What happened when you tried my suggestion?