0

İm working on java project (originally android project) that does json parsing

Originally im trying to find page id from title

İ have the following json string that have information about page id but i cant return the page id 3434750

How can i acces array that contains "pageid" "ns" and "title" without knowing key(3434750) ?

How can i do it?

Thanx in advance

json string

sir mordred
  • 161
  • 1
  • 13
  • For future reference, please do not post text as pictures. Only use screenshots for things that cannot be represented in text form. This makes it easier for people to search and copy code/errors/etc. – oldtechaa Jul 10 '16 at 00:18

1 Answers1

1

Get json object "pages" and Loop throw it by using iterator

Iterator<String> iterator = json.keys();
while (iterator .hasNext()) {
    String key = iterator .next();
    try {
        Object value = json.get(key);
    } catch (JSONException e) {

    }
}
Community
  • 1
  • 1
Siarhei
  • 2,358
  • 3
  • 27
  • 63
  • This gives me what? An array or value that i want(pageid:3434750) ? – sir mordred Jul 10 '16 at 07:31
  • if you put into json variable "pages" object it returns you "3434750" - the key of the object. then you need to get object by this key and then you got access to pageid,ns and title – Siarhei Jul 10 '16 at 09:36
  • JSONObject jsonQuery = (JSONObject) jsonObject.get("query"); JSONObject jsonPage = (JSONObject) jsonQuery.get("pages"); and then how can i supply iterator with **jsonPage** ? Sorry man im not familiar with json thats why im asking too much questions..thank you very much again – sir mordred Jul 10 '16 at 11:42
  • smth like that: Iterator iter = jsonPage.keys(); while (iter.hasNext()) { String key = iter.next(); try { jsonObject obj = jsonPage.getJSONObject(key); } catch (JSONException e) {} – Siarhei Jul 10 '16 at 14:24
  • Thank you very much man..and also **Object value** contains 3434750 right? But 3434750 is **Long** type so how can i convert it to/return from **Long** type? – sir mordred Jul 10 '16 at 20:14
  • have you tried getLong https://developer.android.com/reference/org/json/JSONObject.html#getLong(java.lang.String) ? – Siarhei Jul 10 '16 at 20:48
  • Do you mean Long id = value.getLong("pageid"); – sir mordred Jul 10 '16 at 20:57
  • yes, JSONObject value = jsonPage.getJSONObject(key); id = value.getLong("pageid"); – Siarhei Jul 10 '16 at 21:01
  • Ok i tried and if i define JSONObject value = jsonPage.getJSONObject(key); id = value.getLong("pageid"); above the while loop, it gives me "error: undefined "key" in getJSONObject(key)" so i defined it inside of while loop..is it correct right? – sir mordred Jul 12 '16 at 12:21
  • yes, it's ok. but you should keep in mind that you need to process all pages, if several pages come into request(if it ossible for you project) i mean {"333111":, "444223", .... } – Siarhei Jul 12 '16 at 14:04