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.

5
  • Your question is very unclear and confusing. Please show more what did you try, and explain what would stringtwo be used for. Commented Jan 11, 2015 at 10:49
  • @BasileStarynkevitch webservices developed by some one he need if i post json data to server with backslashes.Please look my code Commented Jan 11, 2015 at 10:56
  • Why the type:'get' ? Commented Jan 11, 2015 at 11:05
  • @BasileStarynkevitch in services he using Get Commented Jan 11, 2015 at 11:12
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on Stack Overflow. See "Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?. BTW, it's "Thanks in advance", not "Thanks in advanced". Commented Jan 17, 2015 at 6:01

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for reply.but when i convert it's not add back-word slashes in stringtwo
There are no backslashes. The literal string token for a string is not the string.
webservices developed by some one he need if i post json data to server with backslashes.Please look my code
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/

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.