1

I'm a bit new to this and it's doing my head in! I keep getting this error:

Error parsing data org.json.JSONException: No value for tname

This is the json:

[{"tname":"2"},{"kword":"||ice+skating+rink"}]

And here is my java code:

String result = response.toString();

            try
            {
                JSONArray jArray = new JSONArray(result);

                for (int i = 0; i < jArray.length(); i++) {
                    JSONObject json_data = jArray.getJSONObject(i);
                    Log.i("log_tag", ", type: " + json_data.getString("tname")+
                            ", keyword: " + json_data.getString("kword"));

                    type += json_data.getString("tname");
                    keyword += json_data.getString("kword");
                }

Any help greatly appreciated.

1
  • 2
    The issue doesn't seem to have anything to do with the question in the title. You seem to be able to parse the JSON just fine, you are just not accessing the resulting array/object correctly. The second object doesn't have a property tname. Three possible solutions: a) Make sure your data has all the expected properties. b) Don't access property tname. c) Test the existence of the property before you access it. Commented Mar 27, 2015 at 21:19

1 Answer 1

5

Your second object doesn't have tname. you should check and see if the object has a property before accessing it

if(json_data.has("tname"))    
    type += json_data.getString("tname");
if(json_data.has("kword"))           
    keyword += json_data.getString("kword");
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the swift reply, I just tried that and am still getting the same error? Any Suggestions?
This line: Log.i("log_tag", ", type: " + json_data.getString("tname")+ ", keyword: " + json_data.getString("kword"));
My mistake, I implemented it incorrectly. Thanks a million for your help!
Haha I've been trying, have to wait another minute! :D

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.