1

i have a Rest ful service,.. and i am getting a JSON data like,..

{
"returnCode": "success",
"RecievedData": {
"results": [
  {
    "details": [
      {
        "moredetails": [{
          "id": "123456",
          "price": "129.99",
          "recorded_at": 3223322,
          "lastrecorded_at": 0002020,
          "seller": "google",
          "availability": "Available",
          "currency": "USD"
        }],
        "offers_count": 1,
        "name": "google.com",
        "recentoffers_count": 1,
        "sid": "988008555566649",
        "url": "http://google.com"
      },
      "moredetails"{
                .
                .

               }
      ] details
         { 
         { 
          [
           ]
          }
    "model": "abc",
    "weight": "127",
    "price_currency": "USD",
    "features": {
      .
      '
      }
     "model": "abc",
    "weight": "127",
    "price_currency": "USD",
    .
    .
    .

i am using this example or tutorial for this

and in that its calling json from this url

an dover there he is parsing data with this json object jsonarray = jsonobject.getJSONArray("worldpopulation");

so i am getting a data from a url as above how to parse this much of json data in that i want only 2 or 3 fields of json array,.

Please help ,.. with a example which is working,..for above,..

Whats Going On
  • 1,348
  • 4
  • 19
  • 48
  • 2
    where is your code of parsing the json? – Raghunandan Dec 11 '13 at 10:09
  • 3
    just curious... why without GSON? – Sebastian Breit Dec 11 '13 at 10:11
  • 1
    hi Raghunandan sir.. i given some urls please go through it ,.. and i dont now how to parse above json in this example http://developer-android-tutorial.blogspot.in/2013/07/android-how-to-parse-json-and-show.html – Whats Going On Dec 11 '13 at 10:11
  • 1
    hellow sir in this example http://developer-android-tutorial.blogspot.in/2013/07/android-how-to-parse-json-and-show.html he not used any kind of libs like GSON,.. just parsing the data,... i already prepaired code which is based on the above example so i dont want any changes i want just parsing form url,.. Thanks Perroloco sir,.. – Whats Going On Dec 11 '13 at 10:14
  • 1
    @user2967727 give a try by parsing json and then if you find difficulty post the same here and ask for help. – Raghunandan Dec 11 '13 at 10:15
  • 1
    i already tried sir,.. but i have multiple json objects and json arrays sir i am trying since 5 days,.. and i asked may question every body understand my concept or problem but no right answer,.. on this link you have code sir,..http://developer-android-tutorial.blogspot.in/2013/07/android-how-to-parse-json-and-show.html please help me sir,.. how use my code – Whats Going On Dec 11 '13 at 10:18
  • 2
    this might be helpful http://stackoverflow.com/a/20513759/2389078 – DroidDev Dec 11 '13 at 10:20
  • 1
    hi vishwas sharma sir,.. i asked that process in moring for that kind but for this kind i need any answer,.. – Whats Going On Dec 11 '13 at 10:22
  • I also recommend using a library such as [Gson](https://code.google.com/p/google-gson/) or [Jackson](http://jackson.codehaus.org/) for the task. Here is a [sample post on how to integrate Gson](http://stackoverflow.com/a/11921827/356895). – JJD Dec 11 '13 at 10:23

3 Answers3

1

You can use gson library. There is an example here!

nikinci
  • 380
  • 4
  • 20
0

As it is nested json and You have not specified which fields do you need its difficult to answer so Use this class as a refrence class use method from where You need data to be displayed, It will return the first object of your array.

public class Parser {

    public String getData() {
        String content = null;

        String result = "";
        InputStream is = null;

        // http get
        try {
            HttpClient httpclient = new DefaultHttpClient();

            String webUrl = "your url";

            HttpGet httppost = new HttpGet(webUrl);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();

            is = entity.getContent();

        } catch (Exception e) {
            Log.d("LOGTAG", "Error in http connection " + e.toString());
        }

        // convert response to string
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
            Log.d("LOGTAG", "Result :" + result);
        } catch (Exception e) {
            Log.d("LOGTAG", "Error converting result " + e.toString());
        }

        // parse json data

        try {

            JSONArray jarray = new JSONArray(result);
            JSONObject jobj = jarray.getJSONObject(jarray.getInt(0));
            content = jobj.getString("key");//key=name of the field you require
        } catch (Exception e) {

            Log.d("LOGTAG", e.toString());

        }
        return content;
    }
}
androider
  • 36
  • 5
0

I saw your comment ... you do not want to change your code. but parsing a json yourself is pretty slow in comparison to using Gson..and use of gson is so simple you just need to create simple pojo classes and it will convert whole json into java object which you can use anywhere. this is also a neat way of doing it.here is an example

nitesh goel
  • 6,108
  • 2
  • 26
  • 37