jason
jason

Reputation: 7164

Unexpected result coming from JSON parser

I'm trying to parse a JSON object. For example, there is a part like this in my JSON object :

"Addition":""

When I try to parse this in JSON and log the result from Logcat like this :

        if(myobj.has("Addition"))
        {
            myMaterial.Addition=myobj.getString("Addition");
            Log.d("myobj.getString("Addition")", myobj.getString("Addition"));
        }
        else
        {
            myMaterial.Addition= null;
        }

The result of the Log is this :

D/myobj.getString("Addition")﹕ [ 07-15 09:36:49.987 1221: 1440 V/WindowManager ]

Why am I getting this result instead of empty string? How can I get an empty string for this case? Thanks.

Upvotes: 0

Views: 48

Answers (1)

MarkySmarky
MarkySmarky

Reputation: 1629

Use optString(key, defaultValue) instead of getString. Provide an empty string as the default value - in this case you can also drop the has(..) check.

Upvotes: 1

Related Questions