0

I am starting with the json:

{
    "Key1" : "Value1",
    "Key2" : "Value2"
}

I am then hard-coding this json in a string:

String json = "{ \"Key1\" : \"Value1\", \"Key2\" : \"Value2\" }";

Next I attempt to parse the json:

JSONObject content = null;
try {
    content = new JSONObject(json);
} catch (JSONException e) {
    e.printStackTrace();
    return null;
}       

String key1 = content.optString("Key1", null);

If I look at the hashmap created from the call to JSONObject, it looks correct:

{Key2=Value2, Key1=Value1}

But when I look at the value of the string key1 in the debugger, I get this:

[V, a, l, u, e, 1, U, U, U, U, U, U, U, U, U, U]

Where U appears to be unicode character 25A1 (White Square).

I've also tried the generic get("Key1") method, casting the result to a string and I get the same behavior?!?

4
  • What does the following return? "Value1".equals(key1); Commented Aug 12, 2011 at 18:37
  • Maybe I'm way off here but defining a JSON object like that seems painful with all the escape characters. Have you tried using myJsonObject.put(string key, object value) to see what is stored? Commented Aug 12, 2011 at 18:44
  • @Jack It is painful but it was just a simple hack to troubleshoot. Commented Aug 12, 2011 at 18:51
  • Ahh gotcha - Yes I have noticed that sometimes when debugging a bunch of garbage is appended to my strings. Commented Aug 12, 2011 at 18:57

2 Answers 2

3

I do not see an issue with your code, as this slight modification appears to work:

public static void main(String[] args)
{
    String json = "{ \"Key1\" : \"Value1\", \"Key2\" : \"Value2\" }";

    JSONObject content = null;
    try
    {
        content = new JSONObject(json);
    }
    catch (JSONException e)
    {
        e.printStackTrace();
        return;
    }

    String key1 = content.optString("Key1", null);
    System.out.print(key1 + "end!");
}

The console output says this: Value1end!

When looking at the code in the debugger, I saw this:

enter image description here

If I were to venture a guess, it may just be additional buffer space the debugger is using to hold the String. Have you tried seeing what your code thinks it is?

"Value1".equals(key1); //should return true if everything is working correctly.
Sign up to request clarification or add additional context in comments.

2 Comments

My evaluation is also true. Perhaps it is debugging garbage, I was concerned that once I parse a more complicated object that my arrays and sub-ojbects would become tainted. I may have been too paranoid.
Sometimes it is best to just code it up and see what happens :)
0

optString(java.lang.String key) Get an optional string associated with a key.

you should use

getString(java.lang.String key) Get the string associated with a key.

Or am i missing something?

For more: http://www.json.org/javadoc/org/json/JSONObject.html

Comments

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.