java - Android JSON names order -


i'm using following code parse info site , works expect older in last loop goes out of whack. names() comes out

["569","570","565","566","567","568","562","563","564"]

those number should in numeric order aren't. there way fix this?

import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.io.stringwriter; import java.io.unsupportedencodingexception; import java.net.httpurlconnection; import java.net.url; import java.util.arraylist; import java.util.hashmap; import java.util.iterator; import java.util.linkedlist; import java.util.list; import java.util.map; import java.util.sortedmap; import java.util.treemap;  import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import org.xmlpull.v1.xmlpullparser; import org.xmlpull.v1.xmlpullparserfactory;  import android.annotation.suppresslint; import android.util.log;  public class json {    private string html;    private string version = "version";    private string pagestring = null;    private string urlstring = "http://frc-manual.usfirst.org/a/getallitems/manualid=3";     public volatile boolean parsingcomplete = true;    public json(string page){       this.pagestring = page;    }    public string gethtml(){       return html;    }    public string getversion(){       return version;    }     @suppresslint("newapi")    public void readandparsejson(string in) {       try {          jsonobject reader = new jsonobject(in);            jsonobject head = reader.getjsonobject("data").getjsonobject("subchapter").getjsonobject("3").getjsonobject("children").getjsonobject(pagestring);          html = head.getstring("item_content_text");           if(head.has("children")){              jsonobject children = head.getjsonobject("children");              jsonarray sub1 = new jsonarray(children.names().tostring());              for(int i=sub1.length()-1;i>=0;i--){                  jsonobject children2 = children.getjsonobject(integer.tostring(sub1.getint(i)));                  html = html + "<h2>" + children2.getstring("secdisp")+ " " + children2.getstring("item_name") + "</h2>";                  html = html + children2.getstring("item_content_text");                  if(children2.has("children")){                      jsonobject children3 = children2.getjsonobject("children");                      jsonarray sub2 = new jsonarray(children3.names().tostring());                      html = html + sub2;                      for(int j=sub2.length()-1;j>=0;j--){                          jsonobject children4 = children3.getjsonobject((string) sub2.get(j));                          html = html + "<h3>" + children4.getstring("secdisp")+ " " + children4.getstring("item_name") + "</h3>";                          html = html + children4.getstring("item_content_text");                      }                  }              }          }           jsonobject main  = reader.getjsonobject("data");          version = main.getstring("latestmanualupdate");           parsingcomplete = false;            } catch (exception e) {            // todo auto-generated catch block            e.printstacktrace();         }     }    public void fetchjson(){       thread thread = new thread(new runnable(){          @override          public void run() {          try {             url url = new url(urlstring);             httpurlconnection conn = (httpurlconnection) url.openconnection();             conn.setreadtimeout(10000 /* milliseconds */);             conn.setconnecttimeout(15000 /* milliseconds */);             conn.setrequestmethod("get");             conn.setdoinput(true);             // starts query             conn.connect();          inputstream stream = conn.getinputstream();        string data = convertstreamtostring(stream);        readandparsejson(data);          stream.close();           } catch (exception e) {             e.printstacktrace();          }          }       });         thread.start();          }    static string convertstreamtostring(java.io.inputstream is) {       java.util.scanner s = new java.util.scanner(is).usedelimiter("\\a");       return s.hasnext() ? s.next() : "";    }  } 

the order of keys in jsonobject undefined (see documentation keys()). in practice, means ordered according hash codes, not want. if need keep these items in specific order, either:

  • use different json parser. there plenty available google search; i'd suggest perhaps "push-parser" may appropriate type, guaranteed give items in object 1 after in order defined. json-simple 1 such parser. @ examples 5 & 6 in "decoding examples" page. alternatively, see example 4 how change type of map uses storing json objects in object-model mode, , note linkedhashmap preserves order values added it.
  • sort list of names before use them
  • change json use array of objects contain number rather object numbers keys, arrays (obviously) kept in order appear in original json.

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -