2

I searched across many sources but could not find the answer to this one..

Its a know fact that a JSONObject keys are returned in reverse order.

Is there any way to recurse through a JSONObject in the correct order as appearing in JSON

String json;
JSONObject obj = new JSONObject(json)
Iterator<String> keys = json.keys()   ---> order is reversed

I understand JSONObject is unordered, perhaps there is a way to order it????

JSON is of type below..and the keys start getting recursed from the bottom most styleHint tag

"sections": {


 "1": {
    "1": {
      "1": {
        "title": "xxx",
        "text": "xxx",
        "tags": {
          "audience": {
            "1": {
              "name": "xxx",
              "title": "xxx",
              "id": "xxx"
            }
          },
          "styleHint": {
            "1": {
              "name": "xxx",
              "title": "xxx",
              "id": "xxx"
            }
          }
        }
      },
      "title": "xxx",
      "text": "xxx",
      "tags": {
        "audience": {
          "1": {
            "name": "xxx",
            "title": "xxx",
            "id": "xxx"
          }
        },
        "styleHint": {
          "1": {
            "name": "xxx",
            "title": "xxx",
            "id": "xxx"
          }
        }
      }
    },
    "2": {
      "title": "xxx",
      "text": "xxx",
      "tags": {
        "audience": {
          "1": {
            "name": "xxx",
            "title": "xxx",
            "id": "xxx"
          }
        },
        "styleHint": {
          "1": {
            "name": "xxx",
            "title": "xxx",
            "id": "xxx"
          }
        }
      }
    },
    "title": "xxx",
    "text": "xxx",
    "tags": {
      "audience": {
        "1": {
          "name": "xxx",
          "title": "xxx",
          "id": "xxx"
        },
        "2": {
          "name": "xxx",
          "title": "xxx",
          "id": "xxx"
        }
      },
      "styleHint": {
        "1": {
          "name": "xxx",
          "title": "xxx",
          "id": "xxx"
        }
      }
    }
  },
  "2": {
    "title": "xxx",
    "text": "xxx",
    "tags": {
      "audience": {
        "1": {
          "name": "xxx",
          "title": "xxx",
          "id": "xxx"
        }
      },
      "styleHint": {
        "1": {
          "name": "xxx",
          "title": "xxx",
          "id": "xxx"
        }
      }
    },
    "anchor":"xxx"

  },
  "3": {
    "1": {
      "title": "xxx",
      "text": "xxx",
      "tags": {
        "audience": {
          "tag": {
            "name": "xxx",
            "title": "xxx",
            "id": "xxx"
          }
        },
        "styleHint": {
          "tag": {
            "name": "xxx",
            "title": "xxx",
            "id": "xxx"
          }
        }
      }
    },
    "title": "xxx",
    "text": "xxx",
    "tags": {
      "audience": {
        "1": {
          "name": "xxx",
          "title": "xxx",
          "id": "xxxx"
        }
      },
      "styleHint": {
        "1": {
          "name": "xx",
          "title": "xxx",
          "id": "xxxx"
        }
      }
    }
  }  
}
HungryForKnowledge
  • 289
  • 1
  • 8
  • 18

1 Answers1

0

How about that

Get your org.json.JSONObject source and change these

public ListIterator keys() {
    ListIterator iter = new ArrayList(this.keySet()).listIterator();
    return iter;
}

and

/**
 * Construct an empty JSONObject.
 */
public JSONObject() {
    this.map = new LinkedHashMap();
}

and

/**
 * The map where the JSONObject's properties are kept.
 */
private final LinkedHashMap map;

and

public JSONObject(Map map) {
    this.map = new LinkedHashMap();
    if (map != null) {
        Iterator i = map.entrySet().iterator();
        while (i.hasNext()) {
            Map.Entry e = (Map.Entry) i.next();
            Object value = e.getValue();
            if (value != null) {
                this.map.put(e.getKey(), wrap(value));
            }
        }
    }
}

I hope I don't burn in hell for that.

Leo
  • 6,480
  • 4
  • 37
  • 52