Sababado
Sababado

Reputation: 2532

Android Decode JSON Encoded by PHP file

I've created a JSON string in a php file. I've then used json_encode($jsonStr) to encode the string.

$jsonStr = 
            "{
                \"statusCode\": 0,
                \"errorMsg\": \"SUCCESS\",
                \"id\": $id,
                \"message\": ".json_encode($message).",
                \"author\": \"$author\",
                \"showAfter\": \"$date\"
            }";

I'm making a network call in java (Android) to get this string. My next step is to decode the string, however this doesn't seem to be working too well.

Here is a sample of what I'm trying to decode in my Android code:

{\n\t\t\t\t\"statusCode\": 0,\n\t\t\t\t\"errorMsg\": \"SUCCESS\",\n\t\t\t\t\"id\": 1,\n\t\t\t\t\"message\": \"This is a message.\",\n\t\t\t\t\"author\": \"Anonymous\",\n\t\t\t\t\"showAfter\": \"2013-06-18 01:19:49\"\n\t\t\t}

Yes it is riddled with encoded line breaks and such. I assumed that might be the issue so I took those out, however I still have issues, so I'm guessing there must be something bigger going on. I know this is valid JSON because I'm able to decode it and use it in a javascript based website.

How can I accomplish this on Android/Java?

Upvotes: 0

Views: 699

Answers (1)

JB Nizet
JB Nizet

Reputation: 691993

Your original JSON string (the one you show in your first snippet) looks to be valid JSON already. You must not encode it. Encoding it is what makes it invalid JSON, transforming every tab into \t, and every new line into \n.

Read the documentation of json_encode carefully.

Upvotes: 3

Related Questions