JSON.Stringify Adds Quotes To ID [duplicate]

╄→尐↘猪︶ㄣ 提交于 2019-12-11 05:07:48

问题


I'm using JSON.stringify and JSON.parse to edit my JSON file based on changes to an online database. Everything works right, except it is making quotes around a number which is screwing up the JSON file. For example it should be "id": 1 but it is printing out "id": "1". How would I edit the quotes out? I prefer to use JSON.stringify and not an alternative.


回答1:


if you are getting the number from input field, the number or whatever input is always string and therefore its quoted.

To fix that you should add parseInt() for your input values like:

var value = parseInt($('#fieldID').val());

Hope that helps




回答2:


1 must not be a true integer to begin with. Executing JSON.stringify({id: 1}) in the console will return "{"id":1}". How are you defining the value for id? I'm guessing at that point, it is getting saved as a string (i.e. {id: "1"}).



来源:https://stackoverflow.com/questions/20890681/json-stringify-adds-quotes-to-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!