0

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?

2
  • Thats not valid JSON... And show us your complete PHP file. What is the content of $jsonStr to what are you assigning it? How do you transfer the json string etc. Commented Jun 30, 2013 at 20:54
  • I realize it isn't valid as is, however before encoding/decoding it is. One sec i'll post more details Commented Jun 30, 2013 at 21:03

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the answer. I am using json_encode to work with a my web app, however I guess my bigger issue is how i'm using it there. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.