How to use back slash in android json

I had a scenario where I need to give back slash for a key in JSON put method like below

json.put("path" , " \\abx\2010\341\test.PDF");

The value I gave for path key shows error.

How to handle this case?

Upvotes: 0

Views: 731

Answers (5)

Gayan Chinthaka
Gayan Chinthaka

Reputation: 569

If you want to show like this, JSON file should be as below.

"path" : " \\abx\2010\341\test.PDF"

JSON also \ represent as \ in java special characters. Then java code should be as below.

json.put("path" , " \\\\abx\\2010\\341\\test.PDF");

Upvotes: 0

Atef
Atef

Reputation: 1322

You need to write double slash instead of one: \\ So your code become:

json.put("path" , " \\\\abx\\2010\\341\\test.PDF");

You can learn more about escaping special characters in this answer.

Upvotes: 2

Saiful Alam Rifan
Saiful Alam Rifan

Reputation: 476

double \\ = single \ in string ""

json.put("path","\\abx\\2010\\341\\test.PDF");

Upvotes: 0

Raghav Sood
Raghav Sood

Reputation: 82553

You need to escape \.

Try json.put("path" , " \\abx\\2010\\341\\test.PDF");

You can learn more about it under Escape Sequences.

Upvotes: 0

Adizbek Ergashev
Adizbek Ergashev

Reputation: 769

Try like this

json.put("path" , "\\abx\\2010\\341\\test.PDF");

Upvotes: 1

Related Questions