Ranoy
Ranoy

Reputation: 108

java script to json

If I have an array like

price=["1@1000", "1000@2000"]

how to convert it into JSON so that it can be send into ajax call of jQuery

$.ajax({
    type: 'POST',
    url: '',
    data: {
        'price': price
    },
    dataType: 'JSON',
    success: function(data) {
        console.log("success");
        console.log(data);
        var products = data.products;
        console.log(products);
    },
});

Upvotes: 0

Views: 72

Answers (2)

OptimusCrime
OptimusCrime

Reputation: 14863

Since you already posted...parts of jQuery, here is a jQuery plugin that should do it

http://plugins.jquery.com/plugin-tags/stringify

|EDIT| The jQuery-plugins-site is put down for a while.

Anyways, you a looking for a function called Stringify. You can read more about it here:

http://www.json.org/js.html

A simple google-search should give you plenty results.

Upvotes: 1

Kræn Hansen
Kræn Hansen

Reputation: 1565

When writing price=["1@1000", "1000@2000"] you already have your data represented as a javascript array. It should be possible for you to simply pass this as an argument as you have described within your use of the $.ajax method. Alternatively (if you really need to parse price as a json object) see the jQuery builtin function for this: http://api.jquery.com/jQuery.parseJSON/

But recheck that this is not just possible, as you have described it, if not, what errors are you getting?

Upvotes: 0

Related Questions