0

I have this JSON array:

[{"id":"101","title":"Oferta 1"},{"id":"102","title":"Oferta 2"},{"id":"103","title":"Oferta del Mes"},{"id":"104","title":"Promoci\u00f3n Facebook"}]

I need to parse this JSON, but when I parse it I recive only

{"id":"101","title":"Oferta 1"}

This is my code:

try {
    JSONObject json = JSONfunctions.getJSONfromURL("URLOFJSON");
    Log.i("log_tag", json.toString()); 
    String jsonvalues =  json.getString("id");

    Log.i("log_tag", jsonvalues);  
}
catch (Exception ex)
{
    Log.e("log_tag", "Error getJSONfromURL "+ex.toString());           
}

How can I resolve this?

Thanks for all.

4
  • You're parsing your response as a JSONObject instead of a JSONArray. Parse it as a JSONArray. Commented Sep 20, 2012 at 16:48
  • Don't know about your JSONFunctions, but looks as if you trying to put a json array into a json object. Isn't there something like: JSONfunctions.getJSONArrayFromURL? Commented Sep 20, 2012 at 16:48
  • hey use the code below, n if it works accept the answer Commented Sep 21, 2012 at 5:04
  • Hi, my problem was the JSONFunctions, this class don't had a getJSONArrayFromURL only has a getJSONObjectFromURL and obtain data was wrong, only obtain the first record Commented Sep 21, 2012 at 11:50

3 Answers 3

1

The problem is you are taking a jsonObject, take a json array and from that retrieve json objects Dummy code

JSonArray ja;
int resultCount = ja.length();
for (int i = 0; i < resultCount; i++)
{
    JSONObject resultObject = ja.getJSONObject(i);
    String id = resultObject.getString("id");

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

Comments

0

for getting the data from the json data use the following line of codes.

String my_info = investors.getProfileAbout(user_id).toString();
            LinearLayout about =(LinearLayout) findViewById(R.id.prof_about);
            about.setVisibility(View.VISIBLE);
            try {
                JSONArray array = new JSONArray(my_info);
                for (int i = 0; i < array.length(); i++) {
                    JSONObject row = array.getJSONObject(i);
                    //id = row.getInt('id');
                    String type1 = row.getString("type");
                    String person_marks = row.getString("follwed");
                    String total_posts = row.getString("totalpost");
                    String componsated = row.getString("ipost");
                    String mods = row.getString("mods");
                    String  doj = row.getString("bday");

                    String avatar = row.getString("avatar");


                   TextView type = (TextView) findViewById(R.id.mebership);
                   type.setText(type1);

                   TextView marks = (TextView) findViewById(R.id.marks);
                   marks.setText(person_marks);

                   TextView total_pos = (TextView) findViewById(R.id.total_posts);
                   total_pos.setText(total_posts);

                   TextView componsated_posts = (TextView) findViewById(R.id.componsated_posts);
                   componsated_posts.setText(componsated);

                   TextView moderating = (TextView) findViewById(R.id.moderating);
                   moderating.setText(mods);

                   TextView user_name = (TextView) findViewById(R.id.profile_username);
                   user_name.setText(extras.getString("user_name"));

                   ImageView userProfilePic = (ImageView) findViewById(R.id.user_profile_img);
                   userProfilePic.setImageBitmap(mySession.downloadImage(avatar));

                   TextView dob = (TextView) findViewById(R.id.dob);
                    dob.setText(doj);
                }

                } catch (Exception e) {
                Log.e("Exception", "Exception when parsing response JSON."+e.getMessage());
                }

For more detail click here http://grabcodes.blogspot.com

Comments

0

I solved with this code.

URL urlws = new URL(
                "URLOFFJSON");
        URLConnection tc = urlws.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                tc.getInputStream()));
        String line;

        while ((line = in.readLine()) != null) {
            JSONArray ja = new JSONArray(line);
            for (int i = 0; i < ja.length(); i++) {
                JSONObject jo = (JSONObject) ja.get(i);
                Log.i("log_tag", jo.toString());
            }
        }

This way I can get the webservice JSONArray

Thanks for all

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.