How to Parse only some selected Json arrays and Json Objects from a Url to android device

StackOverflow https://stackoverflow.com/questions/20492940

  •  31-08-2022
  •  | 
  •  

Domanda

I have a Json data and which contains multiple JSON arrays and JSON Objects so i want to parse some of them in an android activity ,...

Example of JSON array and Json objects at here

So i want to call some of them in the android device.... i know how to parse but i want only some of contents ,..

I have the data like

 {
   "process":"done"
    "one":1
   "List": {
    "Something": [
        {
            "Name": "John",
            "phone": "test"
        }
    ]
     "details":"ok"
     "two":2 
    "SomethingElse": [
        {
            "Name": "Smith",
            "phone": "test"
        }
    ]
   }
  }

Like that i have a Restful service and it has lot of data ,.. so if want to call "list" or "SomethingElse" jsonObjecs/arrays.... its not calling its strucking at starting,..

È stato utile?

Soluzione

You could consider parsing your data the SAX way, just go through your nodes, if it's what you need parse it, otherwise skip it and go on. Have a look here, JSONReader is the way to go.

Altri suggerimenti

Json Array for your above response will be like this

     JSONObject JObject = new JSONObject(response);
      String Process = JObject.getstring("process");
    String one= JObject.getstring("one");
    JSONObject Listobject= JObject.getjsonobject("List");
    JsonArray something =Listobject.getjsonarray("Something");
    for(int i = 0 ; i < something.length(); i++){
     JsonObject somethingobject =something.getjsonobject(i);
         String name=somethingobject.getstring("Name"); 
        String phone=somethingobject.getstring("phone"); 
    }

      String details= JObject.getstring("details");
    String two= JObject.getstring("two");

    JsonArray SomethingElse=JObject.getjsonarray("SomethingElse");

    for(int j = 0 ; j < SomethingElse.length(); j++){
     JsonObject SomethingElseobject =SomethingElse.getjsonobject(j);
         String name1=SomethingElseobject .getstring("Name"); 
        String phone1=SomethingElseobject .getstring("phone"); 
    }

Code for getting data from server

public void run() {

        Log.i("run method", "calling run method");
        try {
            if (method == HttpMethodType.GET) {
                response(executeHttpGet());
            } else {
                response(executeHttpPost());
            }
        } catch (ConnectTimeoutException ex) {
            exception("Please retry after sometime...");
        } catch (UnknownHostException e) {
            exception("Server might be down...");
        } catch (IOException e) {
            exception("Please check your internet connectivity...");
        } catch (Exception e) {
            exception(e.getMessage());
        }

        Log.i(tag, "Http Call Finish");
    }

    private void response(String response) {

        if (resListener != null) {
            resListener.handleResponse(response);
        }
    }

    private void exception(String exception) {
        if (excepListener != null) {
            excepListener.handleException(exception);
        }
    }

    public String executeHttpGet() throws Exception {
        Log.i("calling method", "calling execute");
        Log.i("path in method", path);
        BufferedReader in = null;
        String page = null;
        try {

            HttpGet request = new HttpGet(path);
            HttpResponse response = client.execute(request,localContext);

            Log.i("======", response.toString());
            in = new BufferedReader(new InputStreamReader(response.getEntity()
                    .getContent()));
            StringBuffer sb = new StringBuffer("");
            String line = "";

            while ((line = in.readLine()) != null) {
                Log.i("the response is ::", line);
                sb.append(line);

            }
            in.close();
            page = sb.toString();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return page;
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top