0

In my application i am trying to retrieve the values from a JSON URL. In the URL there is a point where I am not able to parser the values.. I have tried alot to find the solution but I haven't got any..

In the URL the values that I want to parse is the values of tag_users...

I am attaching the code where I am trying to parser the values so, please help me in finding the solution...

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

String url = "http://flutterr.pnf-sites.info/api/messageshow/2";
        JsonParser jParser = new JsonParser();
        JSONObject json = jParser.getJSONfromUrl(url);
        try
        {
            JSONArray messageshow = json.getJSONArray("messageshow");
            userList.clear();
            for(int i=0; i<messageshow.length();i++)
            {
                JSONObject msg = messageshow.getJSONObject(i);
                message_id = msg.getString("message_id");
                message = msg.getString("message");
                message_pic = msg.getString("message_pic");
                uid = msg.getString("uid");
                created = msg.getString("created");
                username = msg.getString("username");
                first_name = msg.getString("first_name");
                last_name = msg.getString("last_name");
                profile_pic = msg.getString("profile_pic");
                total_likes = msg.getString("total_likes");
                JSONObject tag_user = msg.getJSONObject("tag_user");
                message = tag_user.getJSONObject("tags").getString("message");
                if(message != "false")
                    tag_uid = tag_user.getJSONObject("tags").getString("tag_uid");
                Log.v("uid", tag_uid);
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("message_id", message_id);
                map.put("message", message);
                map.put("message_pic", message_pic);
                map.put("uid", uid);
                map.put("created", created);
                map.put("username", username);
                map.put("first_name", first_name);
                map.put("last_name", last_name);
                map.put("profile_pic", profile_pic);
                map.put("total_likes", total_likes);
                userList.add(map);
            }
        }
        catch(JSONException e)
        {
            Log.e("Error", e.toString());
        }

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Please help me in letting me know that how can I parser the values of tag_uid, tag_uname, tag_fname and tag_lname...

1
  • Maybe add an example of your XML, might help people. Commented Oct 15, 2012 at 17:50

2 Answers 2

2

try

if(!message.equals("false"))

instead of

if(message != "false")

One more thing check your server side code

in tag_user there is Json Object with name tags in case of zero tags. And it is (tags) is a JsonArray in case of results.

EDIT

if you Json will look like this

   "tag_user": {
  "tags": [
    {
      "tag_uid": "1",
      "tag_fname": "abhishek",
      "tag_lname": "dhiman",
      "tag_uname": "abhishek"
    }
  ]"message": "true" //here true or false to check "tags" array
}

and then in code you can get like

message = tag_user.getJSONObject("message");

                    if(!message.equals("false"))
                    tag_uid = tag_user.getJSONArray("tags").getJSONObject(0).getString("tag_uid");

//will return "tag_uid" of first tag`

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

5 Comments

can you please tell me how can I get the values of tag_uid, tag_uname, tag_fname and tag_lname.. rest I have done... and thanks
see my edit. you need to make the server response consistent, first. I hope you get what I want to tell you?
i have tried your code but I am getting this error: org.json.JSONException: Value [{"tag_lname":"dhiman","tag_uname":"abhishek","tag_fname":"abhishek","tag_uid":"1"},{"tag_lname":"tttttt","tag_uname":"tttttt","tag_fname":"tttttt","tag_uid":"4"},{"tag_lname":"qqqqqq","tag_uname":"qqqqqq","tag_fname":"qqqqqq","tag_uid":"3"},{"tag_lname":"Lastname","tag_uname":"Username4","tag_fname":"Firstname","tag_uid":"13"}] at tags of type org.json.JSONArray cannot be converted to JSONObject
the code that i have tried is this: JSONObject tag_user = msg.getJSONObject("tag_user"); message = tag_user.getJSONObject("tags").getString("message"); if(!message.equals("false")) { tag_uid = tag_user.getJSONArray("tags").getJSONObject(i).getString("tag_uid"); Log.v("tags", String.valueOf(tag_uid)); }
you haven't read my answer completely. your tags is a JsonObject in some cases and it is JsonArray in some other cases. So first make them consistent.
0

tag_user is a JSON object that contains an array :

"tag_user":{"tags":[{"tag_uid":"1","tag_fname":"abhishek","tag_lname":"dhiman","tag_uname":"abhishek"},{"tag_uid":"4","tag_fname":"tttttt","tag_lname":"tttttt","tag_uname":"tttttt"},{"tag_uid":"3","tag_fname":"qqqqqq","tag_lname":"qqqqqq","tag_uname":"qqqqqq"},{"tag_uid":"13","tag_fname":"Firstname","tag_lname":"Lastname","tag_uname":"Username4"}]}

you should try

getJSONArray("tags")

on tag_user object

If you're familiar with Sax parsing try JsonReader from the Gson project : https://sites.google.com/site/gson/streaming

2 Comments

can you provide me some code so that it can be more clear to me that how can i use getJSONArray("tags");...
I have tried using JSONArray tags = tag_user.getJSONArray("tags"); but I am getting the following error: org.json.JSONException: Value [{"tag_lname":"dhiman","tag_uname":"abhishek","tag_fname":"abhishek","tag_uid":"1"},{"tag_lname":"tttttt","tag_uname":"tttttt","tag_fname":"tttttt","tag_uid":"4"},{"tag_lname":"qqqqqq","tag_uname":"qqqqqq","tag_fname":"qqqqqq","tag_uid":"3"},{"tag_lname":"Lastname","tag_uname":"Username4","tag_fname":"Firstname","tag_uid":"13"}] at tags of type org.json.JSONArray cannot be converted to JSONObject... how can I get the values?

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.