简体   繁体   中英

Reactjs : Remove outer quotes from JSON after using JSON.stringify

I am using JSON.stringify on a Json data and after doing this I am getting the " (quotes) at the starting and ending of the data. For eg :

x = {fieldId: 536, value: {value: 341, display_value: "ABCD"}}

after using JSON.stringify i am getting :

x = "{"fieldId":536,"value":{"value":341,"display_value":"ABCD"}}"

but the result I desired is "

x = {"fieldId":536,"value":{"value":341,"display_value":"ABCD"}}

I have tried JSON.parse after stringify but of no use.

If you want an object

In the first code block in your question, you show your JS source code.

In the third code block, you show some more JS source code and say that it is what you want.

The two bits of source code give identical results. They just use very slightly different JavaScript syntax (in an object literal a property name can be an identifier (like foo ) or a string (like "foo" ), which you use makes no difference to the end result).

If you really do want what is in the third code block, then do nothing .

Do not use JSON.stringify .


If you want JSON

Your second block shows what you say you get after using JSON.stringify .

This result is impossible.

The most likely explanation for this is that you are inspecting the result using a tool which displays a quote before and after the data as a means to indicate to you that the value is a string. The quote characters are not part of the data. You are just misinterpreting what you are seeing.

If you really want a JSON representation of the data in the first code block, then just use JSON.stringify .

 var x = {fieldId: 536, value: {value: 341, display_value: "ABCD"}}; var json = JSON.stringify(x); document.write(json); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM