Link to home
Start Free TrialLog in
Avatar of Jay Roy
Jay RoyFlag for United States of America

asked on

json string error

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
Avatar of StingRaY
StingRaY
Flag of Thailand image

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

Avatar of Jay Roy

ASKER

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
In your java side, you should put \\n instead of \n.
Avatar of Jay Roy

ASKER

Ok will try that.. Can u plz tell me what is the difference between \\n and Single slash n
Thanks
\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.
Avatar of Jay Roy

ASKER

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
                   
Avatar of Jay Roy

ASKER

any idea what i am doing wrong
ASKER CERTIFIED SOLUTION
Avatar of StingRaY
StingRaY
Flag of Thailand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jay Roy

ASKER

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
If use \\n, what does it look like?
Avatar of Jay Roy

ASKER

if i use \\n
it looks like

10021\nINOPS-RAF +MONEY\nINDIA  
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jay Roy

ASKER

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
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jay Roy

ASKER

I am actually using that json data to disply on a flex UI..so whats happening is the slashes are getting displayed as is.
What happened when you tried my suggestion?