6

I am using JSON to send Ajax data. I am getting comma separated mobile number from the input text box.And, I am converting it into javascript array.
Below is my code:

  var myarray = {};
    myarray = this.model.get('mobileno').split(',');

Result : myarray : ["123", "4567"];

I am going to set the same value to my model like below:

this.model.set('mobileno',JSON.stringify(myarray ));

Then, the value becomes like below:

console.log(this.model.get('mobileno'));

Result : mobileno : "["123","4567"]"

So, my model become this.model.toJSON();

Result :Object {mobileno: "["123","4567"]}

Till here, everything is correct. after that I need to set this model to another model and doing stringfy will give me like Below:

 anotherModel.set('data', this.model);

"data":{"mobileno":"[\"123\",\"456\"]"}

But, I need like "data":{"mobileno":["123","456"]}

Your help will be appreciated.

4
  • 2
    Why do you convert the array back to JSON? Commented Jul 5, 2015 at 9:37
  • in my textbox i am having comma separated mobile number like 9845098450,9845098451 etc.. when i get this from model i will get like "9845098450","9845098451" like this. i need to make request like {data:{mobileno:["9845098450","9845098451"]}} so how could i achive this, Commented Jul 5, 2015 at 9:40
  • So? Why do you need JSON for that? Commented Jul 5, 2015 at 9:41
  • JSON arrays are not strings. They are actually JavaScript arrays. That was an design goal of JSON. So, if you need a string of a JavaScript/JSON array, wait until you've finished building the array before you stringify it. Commented Jul 5, 2015 at 10:17

1 Answer 1

7

JSON.stringify makes a string from your array. That is obviously not what you want. Or it is what you want in this.model as you said

Till here everything is correct.

but in the other model, you want to set the array not as a string, but as the array. As I don't know what you are doing with your backbone.js I write it as pure javascript

data = JSON.parse(this.model.get("mobileno"))

should do the job. But you can just set

data = { "mobileno": myarray }

BTW. if the backbone.js does nothing more than confusing the javascript object and array notation, I would recommend not to use it at all. As you told us the backbone.js this.model.get('mobileno') returns an object containing the mobileno field. In my world of logic anything.get('XY') should return the value of XY not an object containing the XY property.

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

2 Comments

I did like this. Its working. but what is the problem with the my code?
There is no problem with your code but with the way you look on javascript objects, strings and JSON. Actually JSON is just the 'Java Script Object Notation', thus a JSON.stringify(ARG) just returns a string, whose contents can be copied into javascript text to write down the status of the object. So if you say obj=JSON.stringify(obj) the obj will be a string, regardless of what obj was before. If obj was a string it will be a string, that contains a quoted string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.