-1

I have a collection of keys in my jsonObject and i need to iterate through this collection. Code copied from https://stackoverflow.com/a/17399110/1264217 And my code is:

public static void parseJson(JSONObject jsonObject, String dir) throws ParseException {

    //Set<Object> set = jsonObject.keySet();
    ArrayList<JSONObject> arrays=new ArrayList<JSONObject>();
    Iterator<Object> iterator = jsonObject.keys();
    while (iterator.hasNext()) {
        Object obj = iterator.next();
        try {
            if (jsonObject.get((String) obj) instanceof JSONArray) {
                System.out.println(obj.toString());
                getArray(jsonObject.get((String) obj));
            } else {
                if (jsonObject.get((String) obj) instanceof JSONObject) {
                    dir=dir+(String)obj+"/";
                    parseJson((JSONObject) jsonObject.get((String) obj),dir);
                    dir=dir.replace((String)obj+"/","");
                } else {
                    String path=dir+obj.toString();
                    System.out.println(obj.toString() + "\t"
                            + jsonObject.get((String) obj));
                }
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

After the execution of First line it shows me my collection of 4(which is correct) but "LastEntryreturned" points to last key and "next()" to null so after second line code it ends the loop and returns result without iterate over the collection.

This code was working good 2 days ago but i dont know what happened with it now. Please help me i have already spent too much time on it... Watch of my iterator variable

Community
  • 1
  • 1
Zaid Iqbal
  • 1,662
  • 5
  • 25
  • 45
  • Please provide more code. – shmosel Jun 10 '14 at 05:16
  • 1
    We need more detail. What specific behavior did you observe that leads you to believe the loop was skipped? Were you analyzing the execution in a debugger? Do you actually do anything with the keys inside the loop? It'd help to post a [minimal runnable example that lets us see the problem when we run it](http://stackoverflow.com/help/mcve). – user2357112 Jun 10 '14 at 05:17
  • Yes, readers will find it difficult without much code to undertand – Cyclops Jun 10 '14 at 05:17
  • @drewmore LastEntryReturned() is the default function of iterator which shows the back pointer of current. – Zaid Iqbal Jun 10 '14 at 05:21
  • @shmosel Question updated – Zaid Iqbal Jun 10 '14 at 05:22
  • @ZaidIqbal Standard Java Iterator objects have no such function, so I presume it's an addition in the specific implementation of Iterator you are using, and possibly not intended for external use. Where in your code are you using it, and what are you using it for? I don't see it in the sample you've posted. – Jules Jun 10 '14 at 05:24
  • @Jules oki for the time being leave this function my point is that my pointer is not pointing first index of collection after Iterator iterator = jsonObject.keys(); this – Zaid Iqbal Jun 10 '14 at 05:29
  • Where are you seeing incorrect behavior? Are you debugging? Are you expecting a certain output? Can you show us what the input looks like? – shmosel Jun 10 '14 at 05:31
  • @shmosel I am seeing incorrect behavior after this line Iterator iterator = jsonObject.keys(); because after this line iterator point to a third or fourth index on collection. – Zaid Iqbal Jun 10 '14 at 05:36
  • @shmosel input string is too much long. i am just gonna send you a sample {ActionType=COMPLETE, Data={content={jquery.mobile.theme-1.2.0.css=jquery.mobile.theme-1.2.0.css, jquery.flash-0.1.css=jquery.flash-0.1.css, styles.css=styles.css, jqueryfiletree.css=jqueryfiletree.css, jquery.mobile.structure-1.2.0.css=jquery.mobile.structure-1.2.0.css, – Zaid Iqbal Jun 10 '14 at 05:39
  • @shmosel I have copied the code from here http://stackoverflow.com/a/17399110/1264217 – Zaid Iqbal Jun 10 '14 at 05:43
  • You still haven't explained what you're seeing and how. – shmosel Jun 10 '14 at 05:43

1 Answers1

1

Since the problem is not clearly defined, I'll try to lay out a few possibilities that come to mind, with the assumption that you're seeing this problem while debugging:

  1. You're evaluating next() in your debugger, causing the iterator to move ahead outside of your code.

  2. You have a breakpoint near the beginning of the method. If the method gets called recursively with a nested JSON object, the second time you hit the breakpoint it could seem like the number of elements decreased.

  3. You're seeing the "last" JSON field sooner than you expect, because the JSON parser is not maintaining the order of the fields in the input string.

shmosel
  • 49,289
  • 6
  • 73
  • 138
  • I have set a breakpiont at the start of the function so it is impossible that it is not hitting first time – Zaid Iqbal Jun 10 '14 at 05:53
  • I was not suggesting it didn't hit the first breakpoint, rather that you hit "continue" after it did. What is it you see when you hit that breakpoint? – shmosel Jun 10 '14 at 05:54
  • When my breakpoint is hit first time, iterator already goes to last or second last index – Zaid Iqbal Jun 10 '14 at 05:59
  • Explain what you mean by "last index". Iterators don't have an index. – shmosel Jun 10 '14 at 06:00
  • How do you identify the last entry? – shmosel Jun 10 '14 at 06:15
  • By debugging... like i have an iterator and it shows [0]... [1]... [2]... [3]... I added this iterator to watch and check which entry it points to – Zaid Iqbal Jun 10 '14 at 06:21
  • Like I said, iterators don't have indexes. How are you seeing [0]? What exactly are you watching? – shmosel Jun 10 '14 at 06:23
  • Yes Exactly... now from here the problem starts... Strange thing is i did this code on Friday and it was working perfect suddenly when i check its output tomorrow it gives me nothing – Zaid Iqbal Jun 10 '14 at 06:52
  • I see 4 entries in your screenshot. What's the problem exactly? – shmosel Jun 10 '14 at 06:55
  • Now the problem is that on first iteration when i get the values from iterator, instead of giving me first entry value it gives me the last entry value and on next iteration iterator stopped looping.. – Zaid Iqbal Jun 10 '14 at 07:15
  • 1
    The iterator doesn't have to move in order of the table index. I suspect you're either imagining or overlooking your problem because you're focusing too much on implementation details of `HashMap`. You can rest assured that Java's `HashMap` does what it's supposed to do. If there's a problem somewhere, it's either a misinterpretation, as I suggested in my answer, or there's a bug in `parseJson`. – shmosel Jun 10 '14 at 07:24