Zapnologica
Zapnologica

Reputation: 22556

Convert JSONObject which contains \" to a normal string with JSON

I receive a string from an http request which contains data such as:

{"status":1,"Type":3,"Data":"<p style=\"padding-left:80px\"><\/p><ol><li><span style=\"color:#ff0000\">This<\/span><\/li><li>is a<\/li><li><strong>m<span style=\"background-color:#66cc00\">are<\/span><\/strong><\/li><\/ol><p><\/p><p style=\"padding-left:80px\"><strong style=\"text-align:left\"><span style=\"background-color:#66cc00\"><\/span><\/strong><\/p>  "}

I convert it to a JSONObject like so:

jsonObj = new JSONObject(result);

I then need to get the html as a String to display in a TextView,

I have tried this:

String data = jsonObj.getString("data");

but data remains null. This works with simple json strings, but i think it might be cause of the " characters.

Upvotes: 0

Views: 1069

Answers (2)

Kaidul
Kaidul

Reputation: 15885

You have used "data" instead of "Data". This is the only silly mistake you did. To avoid such type of typo mistake, always user final static String to access them from anywhere.

final static String KEY_DATA = "Data";

Then access it inside your class (suppose class name is Aclass):

jsonObj.getString(KEY_DATA);

And in other classes:

jsonObj.getString(Aclass.KEY_DATA);

This is a good practice indeed and no possibility of typo mistake!

Upvotes: 3

Paul
Paul

Reputation: 141877

You are using "data" with a lowercase d, but your JSON contains "Data" with a capital D. Use this:

jsonObj.getString("Data");

Upvotes: 6

Related Questions