Java – Type Safety warning with JSON Iterator

androidjavajson

My problem comes from getting an Iterator from a JSONObject.

Code generating error in its simplest form:

String json = client.retrieveList();
JSONObject jsonList = new JSONObject(json);
Iterator<String> i = jsonList.keys();

while(i.hasNext())
{
    String next = i.next();
    JSONArray jsonArray = jsonList.getJSONArray(next);
    // Do stuff with jsonArray, example: jsonArray.getString(0), jsonArray.getString(1);
}

The exact warning is:
Type safety: The expression of type Iterator needs unchecked conversion to conform to Iterator

So the question is how can I eradicate this warning?

Many thanks!

Best Answer

i realize this is an old thread, but for future searchers...

you can also infer a generic and cast the returns of the iterator methods...

Iterator<?> i = jsonList.keys();

while(i.hasNext())
{
    String next = (String) i.next();
    ...