• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 96
  • Last Modified:

JSON Parse Java-Script

Hello,
I have a question about Java-Script and JSON. It looks like that JSON.parse in Java-Script has many problems with escaped strings.
Anybody knows a solution how to work in Java-Script with JSON strings that works everywhere but not in Java-Script?

[{"id_case":"589","body_case":"<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http:\/\/schemas.xmlsoap.org\/soap\/envelope\/\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"  xmlns:tns=\"http:\/\/kd.blum.com\/api?ws=1\"><soap:Body><tns:initiateCorpus><initiateCorpus><ruleset>MR_Ruleset_Placeholder<\/ruleset><\/initiateCorpus><\/tns:initiateCorpus><\/soap:Body><\/soap:Envelope>"},{"id_case":"590","body_case":"<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http:\/\/schemas.xmlsoap.org\/soap\/envelope\/\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"  xmlns:tns=\"http:\/\/136.243.36.137:8082\/api?ws=1\"><soap:Body><tns:addFront><addFront><corpus><id>MR17090144d5a674a762c4a1c7f8e2afa838cb0559ad1fe8dda2b<\/id><\/corpus><containerNumber>1.1.1<\/containerNumber><position>front<\/position><\/addFront><\/tns:addFront><\/soap:Body><\/soap:Envelope>"},{"id_case":"591","body_case":"<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http:\/\/schemas.xmlsoap.org\/soap\/envelope\/\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"  xmlns:tns=\"http:\/\/136.243.36.137:8082\/api?ws=1\"><soap:Body><tns:getCorpus><getCorpus><corpus>MR17090144d5a674a762c4a1c7f8e2afa838cb0559ad1fe8dda2b<\/corpus><\/getCorpus><\/tns:getCorpus><\/soap:Body><\/soap:Envelope>"},{"id_case":"592","body_case":"<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http:\/\/schemas.xmlsoap.org\/soap\/envelope\/\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"  xmlns:tns=\"http:\/\/136.243.36.137:8082\/api?ws=1\"><soap:Body><tns:alterMultipleCorpusParameter><alterMultipleCorpusParameterById><corpus>MR17090144d5a674a762c4a1c7f8e2afa838cb0559ad1fe8dda2b<\/corpus><parameterValues><items><key>CorpusTopPanel_space_above<\/key><value>-1<\/value><type>Grundeinstellungen<\/type><\/items><\/parameterValues><\/alterMultipleCorpusParameterById><\/tns:alterMultipleCorpusParameter><\/soap:Body><\/soap:Envelope>"},{"id_case":"593","body_case":"<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http:\/\/schemas.xmlsoap.org\/soap\/envelope\/\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"  xmlns:tns=\"http:\/\/136.243.36.137:8082\/api?ws=1\"><soap:Body><tns:getCorpus><getCorpus><corpus>MR17090144d5a674a762c4a1c7f8e2afa838cb0559ad1fe8dda2b<\/corpus><\/getCorpus><\/tns:getCorpus><\/soap:Body><\/soap:Envelope>"}]

Open in new window

In Java-Script only the Ruleset value "MR_Ruleset_Placeholder" is shown finally.
0
Ingo Foerster
Asked:
Ingo Foerster
2 Solutions
 
leakim971PluritechnicianCommented:
your JSON look fine, why do you want to parse it ? how do you receive the data ?
0
 
Ingo FoersterProgrammerAuthor Commented:
I try to load the data to a ui control. This control use JavaScript JSON.parse. And this makes me trouble. I really do not understand this, so I hope there is a workaround for this.
0
 
Flabio GatesCommented:
Try doubling your backslashes. See fiddle here.
var s = '[{"id_case":"42","body_case":"<?xml version=\\"1.0\\" encoding=\\"utf-8\\"?>"}]';
var d = JSON.parse(s);

Open in new window

0
 
Julian HansenCommented:
According to this site https://jsonformatter.curiousconcept.com/ your JSON is valid.

It all depends on how your JSON is getting into your application. If you put that JSON into a file data.json and then load it with jQuery's getJSON() function there is no problem with it.

If you try and assign it to a JavaScript variable and then pass that to JSON.parse it will have a problem because of the \" in the string

Bottom line - if you are passing that JSON as a string declared within the application you will have to escape the strings as pointed out in Flabio's post
0
 
Ingo FoersterProgrammerAuthor Commented:
Thank you very much, this helped me a lot
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now