0

I have JSON like this

var myVariable=[{"items": "SOFT TOUCH AVOCADO 125 ML x 6D","descriptions": "100mg","umo": "CTN","qty": 1,"price": 0,"lineamount": 0,"customerid": "ANG001","tdate": "11:0:2015","orderId": "Himansu12:9:707","bookorder": "ABCANG00197","FOC": 1}];

But We need JSON like this

 var stringtwo ="[{\"items\": 19760,\"descriptions\": 7,\"umo\": 18377,\"qty\":8,\"price\":8,\"lineamount\": \"22\" ,\"customerid\": 1960,\"tdate\": 7,\"orderId\": 1377,\"bookorder\":7,\"FOC\":7 }]";

so Please any one guide me how to convert JSON (myVaiable) to JSON (stringtwo).We Try to POST data to server. Webservice developed by .NET.SO We need POST JSON (stringtwo).But We Get JSON (myVaiable).

$.ajax({
            url:'http://183.82.0.221:1234/MyService.svc/PostOrderData/'+stringone+','+stringtwo+'',
            dataType:'jsonp',
            type:'get',
            cache:false,
            timeout: 2000,
            error: function(x, t, m) {
        if(t=="timeout") {
            alert("this app need internet connection so Please connect net first ");

        } else {
            //alert(t);
        }},
          success:function(data) {
                debugger;
    alert(data);
    console.log(data);
    },
});

We tried JSON data post to Server.but we got 400 bad request.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Pavan Alapati
  • 101
  • 2
  • 11
  • Your question is very unclear and confusing. Please show more what did you try, and explain what would `stringtwo` be used for. – Basile Starynkevitch Jan 11 '15 at 10:49
  • @BasileStarynkevitch webservices developed by some one he need if i post json data to server with backslashes.Please look my code – Pavan Alapati Jan 11 '15 at 10:56
  • Why the `type:'get'` ? – Basile Starynkevitch Jan 11 '15 at 11:05
  • @BasileStarynkevitch in services he using Get – Pavan Alapati Jan 11 '15 at 11:12
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). BTW, it's "Thanks in advance", not "Thanks in advanced". – John Saunders Jan 17 '15 at 06:01

3 Answers3

1

You just want to serialize in a JSON string. Read more about JSON in mozilla.

So use JSON.stringify like

var stringtwo = JSON.stringify(myVariable)

(BTW, you are lacking a Javascript tag)

Notice that you might use it multiply (but I can't guess why you would want to do that), for instance

var stringthree = JSON.stringify(stringtwo)

and even perhaps

var stringfour = JSON.stringify(stringthree)

etc

you could later call jquery.ajax like

$.ajax({ url: "http://some.url/goes/here",
         type: 'POST',
         data: stringfour, // but more probably stringtwo or stringthree
         success: function (gotdata) {
         }
         /// etc
       })

Read also about string literals & code injection

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

Serialize your object as JSON string and replace " with \"

// SiwachGaurav's version from http://stackoverflow.com/questions/1144783/replacing-all-occurrences-of-a-string-in-javascript
function stringReplaceAll(str, find, replace) {
    return str.replace(new RegExp(find.replace(/[-\/\\^$*+?.()|[\]{}]/g,'\\$&'),'g'),replace);
}

var myVariable = [{"items": "SOFT TOUCH AVOCADO 125 ML x 6D","descriptions": "100mg","umo": "CTN","qty": 1,"price": 0,"lineamount": 0,"customerid": "ANG001","tdate": "11:0:2015","orderId": "Himansu12:9:707","bookorder": "ABCANG00197","FOC": 1}];

var stringtwo = stringReplaceAll(JSON.stringify(myVariable), '\"', '\\"');

// stringtwo becomes "[{\"items\":\"SOFT TOUCH AVOCADO 125 ML x 6D\",\"descriptions\":\"100mg\",\"umo\":\"CTN\",\"qty\":1,\"price\":0,\"lineamount\":0,\"customerid\":\"ANG001\",\"tdate\":\"11:0:2015\",\"orderId\":\"Himansu12:9:707\",\"bookorder\":\"ABCANG00197\",\"FOC\":1}]"

jsFiddle demo: http://jsfiddle.net/xmojmr/v6b5ma9n/1/

xmojmr
  • 8,073
  • 5
  • 31
  • 54
0

Here's a trick I've used to do what you are asking (requires Intellij IDEA - not sure if Eclipse will do this). Also, assuming this is a once off, not part of a repeating process:

In any Java file, do this:

String json "";

Now, put the JSON that does not include the backslashes on the clipboard. Position the cursor between the quotes. Paste. Voila. Your JSON is escaped exactly as you requested.