Getting JSON.typeMismatch trying to convert string coming from an HTTP Get to JSON object

I have looked at multiple other answers here but I still have not been able to resolve this issue.

I call a PHP restful server to get a JSON string from a database. Here is the log output of the string that comes back:

"{\"pat_list\":[{\"id\":\"1\",\"name\":\"John Deere\",\"gender\":\"M\",\"child\":\"N\",\"email\":\"mail@site.com\",\"patientid\":\"12657\",\"pridiag\":\"Sick\"}]}"

This looks like valid JSON (it validates) and looks properly escaped to me (it was created using json_encode in PHP on the server)

However, when I try to execute this code:

    if (json_pat_list != null) {

        Log.d("json_string",json_pat_list);

        try {

            JSONObject jsonObj = new JSONObject(json_pat_list);

I get:

W/System.err﹕ org.json.JSONException: Value {"pat_list":[{"id":"1","name":"John Deere","gender":"M","child":"N","email":"mail@site.com","patientid":"12657","pridiag":"Sick"}]} of type java.lang.String cannot be converted to JSONObject ... W/System.err﹕ at org.json.JSON.typeMismatch(JSON.java:111)

Can anyone see what I am missing here?

Answers


Your code seems legit. As far as I can see. What I can suggest is that because you say that it is generated on the PHP server, it might be putting in characters that malformat the string completely.

There are 2 things I can suggest:

    if (json_pat_list != null) {

    Log.d("json_string",json_pat_list);

    //Either this

    String formattedString = json_pat_list.replaceAll("\n", "\\n");

     //or you can do this
     String formattedString = json_pat_list.substring(s.indexOf("{")); 

    try {

        JSONObject jsonObj = new JSONObject(json_pat_list);

Need Your Help

get the texts with xpath query

iphone objective-c ios xpath xpathquery

i want to get the main texts of this page :

How to Change the height of UIPageControl?

iphone objective-c ios uipagecontrol

I am trying to change the height of UIPageControl how I can achieve that?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.