0

Been working on this for a few hours now, not really sure how i should be going about sorting through my test array. I've tried a few different options i have seen posted this one is about the simplest to implement into my project. any suggestions or ideas that might help me get over this hump?

JSON Array from php

{
    "Questions": {
        "0001": {
            "Title": "What is Stackoverflow",
            "Answer": "C",
            "User": "testUSER",
            "Date": "0000-00-00",
            "Used": "0"
        },
        "0002": {
            "Title": "What is Stackoverflow",
            "Answer": "C",
            "User": "testUSERb",
            "Date": "0000-00-00",
            "Used": "1"
        },
        "0003": {
            "Title": "What is Stackoverflow",
            "Answer": "C",
            "User": "testUSERc",
            "Date": "0000-00-00",
            "Used": "0"
        }
    },
    "Count-start": 1,
    "Count-end": 3
}

Used: 0 = not been used 1 = user has used. I will use that to determine whether or not to show the user the question and answer.

Helper Class

public JSONObject query(){
        HttpClient client = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000);
        HttpResponse response;
        JSONObject json = new JSONObject();
        HttpPost post = new HttpPost(QUERY_LINK);
        post.setHeader("json", json.toString());
        StringEntity se;
        try {
            se = new StringEntity(json.toString());
            se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
            post.setEntity(se);
            response = client.execute(post);
            if (response != null) {
                InputStream in = response.getEntity().getContent();
                a = convertStreamToString(in);
                JSONObject check = new JSONObject(a);   
                return check;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return json;
    }

Main Activity

private ArrayList<ListHelper> GetSearchResults(){
        JSONObject getJSON = new Helper().query();
        JSONArray jArrayObject = new JSONArray();
        jArrayObject.put(getJSON);
        int end = 0,start = 0;
        String title, answer, user, used;
        ListHelper sr = new ListHelper();


     ArrayList<ListHelper> results = new ArrayList<ListHelper>();
     JSONObject offerObject = null;
        try {
            offerObject = getJSON.getJSONObject("Questions");
            start = offerObject.getInt("Count-start");
            end = offerObject.getInt("Count-end");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


     for(int a = start; a < end; a = a++) {
         try {


            JSONObject businessObject = offerObject.getJSONObject("Id");
            // this is were i'm stuck, how to go about selecting each id

            title = businessObject.getString("Title");
            answer = businessObject.getString("Answer");
            user = businessObject.getString("User");
            used = businessObject.getString("Used");

            sr = new ListHelper();
             sr.setTitle(title);
             sr.setUser(user);
             sr.setUsed(used);
             results.add(sr);
        } catch (JSONException e1) {
            e1.printStackTrace();
        }
      }
     return results;
    }
}
1
  • Your Json data from php is not a Json Array type.. its completely Json Object. Commented Jul 22, 2013 at 5:26

1 Answer 1

1

You can do the below for your purpose: define a new DecimalFormat outside the for loop

DecimalFormat myFormatter = new DecimalFormat("0000"); 

Then inside the for loop form your id string using the formatter.

String id = myFormatter.format(a);

And then use the id for fetching the JSONObject

// this is were i'm stuck, how to go about selecting each id
JSONObject businessObject = offerObject.getJSONObject(id);

The above will work only if your ids are at max 4 digits. You can make the format dynamic by using the "Count-end" value instead of using hard coded "0000".

All this trouble could have been avoided if "Questions" object were a JSONArray.

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

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.