0

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,..

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the Answer sir,... But by Restful service is not designed from xml,.. Its designed for JSON objects and Json arrays,... SO i want to call them thats it,.. but i want that i need to call any JSON Array or Any JSON object,.. for a Url,,, and that needs to be parsed,.. and in wihch Images are also there Thank you,..
I was pointing to JSON as well, maybe I used the wrong terminology when I said "nodes". Have a look at the example and the JSONReader class.
k sir,... is it from a URL,..if so please provide some more links,... or example for that in android,..
Just to make sure I get the problem right, your json data gets retrieved based on a url request, right? This means you'll get a response with a stream containing your json data. It's that stream that you'll be able to easily parse with JSONReader; If I got it wrong then give me the details to correctly understand your problem.
yes sir,. its from a Restful service,... and i am parsing that data to android ,.. so i used this link,.. developer-android-tutorial.blogspot.in/2013/07/… to get the data from url so he used worldpopulation ,.. if i use list or somthing at a that place its getting error,... Please help me sir,..
|
1

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;
    }

1 Comment

hellow Ravneet sir,... i need to call this from a Restful service i mean from a URL once check this link developer-android-tutorial.blogspot.in/2013/07/… i how i need to access the above data if its coming from a url

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.