JSON Parser/For loop help


  1. KarneeKarnay

    KarneeKarnay Member

    Recently I started developing a app that searches and API to find a game your looking for. A friend of mine lent me his twitter project to edit so I could make my own. I've been modifying it, but the problem I'm having right now is that the for loop is not returning the searched game.

    The image below is an example of what the JSONParser should be getting if the user searched Far Cry 3.

    [​IMG]

    The problem I think is that the for loop is not returning the name value and I currently have no idea why. Anyone know what my problem is?

    [HIGH] private void loadTweetsJSON(String search) throws Exception
    {
    results = downloadTweets("http://api.giantbomb.com/search/?query=" + URLEncoder.encode(search, "UTF-8") + "&field_list=name,id&resources=game,concept&api_key=1234556789&format=json&offset=0");
    System.out.println(results.toString());

    JSONObject jsonResults = new JSONObject(results);
    JSONArray resultsArray = jsonResults.getJSONArray("results");

    listItems = new ArrayList<String>();

    for (int i = 0; i < resultsArray.length(); i++)
    {
    JSONObject j = (JSONObject) resultsArray.get(i);
    listItems.add(j.getString("name") + ": " + j.getString("text"));
    }

    handler.post(displayResultsRunnable);
    }
    [/HIGH]

    EDIT: A quick update. After debugging the for loop I've noticed that the int i value never increases. This leads me to believe that the code is searching through the "results" array, but it is failing to find the value of my search string. Which in this case would be Far Cry 3. Once again though I'm not sure why it's doing this.

    Advertisement
    :
  2. jonbonazza

    jonbonazza Well-Known Member

    If the for loop only executes once, then that means that resultsArray.length() == 1, so the resultsArray only has 1 item in it.

    Messing with raw json is usually best avoided if possible. I highly recommend taking a look at the Twitter4j library. It provides an easy to use high level wrapper around the Twitter apis. It's what we use in all of our apps and seems to be the best one we have found.
  3. KarneeKarnay

    KarneeKarnay Member

    The problem is that i never seems to increase. I assumed that this is because it can not find the name variable. I don't think the for loop works at all, because it's just not finding the item. Either that or it has found it and is simply not converting the text value to the value in the name variable.
  4. jonbonazza

    jonbonazza Well-Known Member

    I think you are misunderstanding how a for loop works. In java, a for loops is really just a glorified while loop. the for loop you posted above is the same thing as the following:

    [HIGH]
    int i = 0;
    while(i < resultsArray.length()) {
    JSONObject j = (JSONObject) resultsArray.get(i);
    listItems.add(j.getString("name") + ": " + j.getString("text"));
    i++;
    }
    [/HIGH]

    As you can see, i will always be updated as long as it enters the loop. The ONLY case where i will not change at all is if resultsArray.length() is equal to 0. In that case, the loop will never be entered and thus i will never be incremented. In short, the way your code is written, if the for loop is entered even once, i will be updated.

    If you enter a log statement inside of the for loop, you can easily check to make sure that it is actually looping. for example, if you change your code the following:
    [HIGH]
    int i;
    for (i = 0; i < resultsArray.length(); i++)
    {
    JSONObject j = (JSONObject) resultsArray.get(i);
    listItems.add(j.getString("name") + ": " + j.getString("text"));
    }
    Log.d("loop_check", "i = " + i);
    [/HIGH]

    you can see exactly what i is when the loop exits. If i is equal to 0, then the loop was never executed.
  5. KarneeKarnay

    KarneeKarnay Member

    So I've done as you suggested and the output has been "0". It never increases. There is stuff in the array it just doesn't seem to get the array's length or it just ignores it. I've used the eclipse debugger to look at the values displayed in the "resultsArray" and it does indeed contain the all the information stored in the "results" array from the API.

    My understanding is because of this problem with "i", it never downloads the contents of the array, and therefor there can't be anything to put as the text value or on the ListView in my program.

    No that I defiantly know the problem is with the for loop, how do I get this to work?

    EDIT: I just found something strange. The length of the array is equal to exactly 1...Not sure how that's possible.

    EDIT2: I've found that the "resultsArray" length is equal to the amount of times there is a "results" json array on the page. The problem now has to be with the fact that the "text" value is not being converted into string associated with the "name :" string. I've taken some screenshots to help.

    [​IMG]

    Once I realised the problem with the "resultsArray.length()" I changed my search query to "Far Cry" instead of "Far Cry 3". That way I got more than one result instead of the 1. I will change the for loop at a later time to fix this.

    [​IMG]

    With this new search query I added the log code you suggested by the problem was that the program never reached it. Instead I had wrote a "catch" method that would pick up any errors and then print it in the log. Below is the output.

    [​IMG]

    EDIT3:

    I feel like the biggest of idiots the reason as to why there was no text value was because it didn't exist. :p There was the reason for the catch activating and for the program to stop working. I'm sorry this took up so much time. Thank you very much for all the help jonbonazza.

    [​IMG]
  6. jonbonazza

    jonbonazza Well-Known Member

    Glad you got it sorted. Sorry I wasn't of more help.
Loading...

Share This Page