Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse the Json response in android?

i m getting this response in a result of GET request to server

{"LL": { "control": "dev/sys/getkey", "value": "4545453030304138303046392035343733373432363020323031332D30322D31312031383A30313A3135", "Code": "200"}}

i just want to extract the value of "value" from the above json response.

I m using this code to get this response

findViewById(R.id.button1).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            HttpResponse response = null;
            try {
                HttpClient client = new DefaultHttpClient();
                HttpGet request = new HttpGet();
                request.setURI(new URI(
                        "http://192.168.14.247/jdev/sys/getkey"));
                response = client.execute(request);
            } catch (URISyntaxException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            String responseText = null;
            try {
                responseText = EntityUtils.toString(response.getEntity());
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Log.i("Parse Exception", e + "");

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Log.i("IO Exception 2", e + "");

            }

            Log.i("responseText", responseText);

            Toast.makeText(MainActivity.this, responseText + "",
                    Toast.LENGTH_SHORT).show();

        }

    });

my question is that how can i parse this and get the value of only "value" tag. thanks

like image 655
Qadir Hussain Avatar asked Feb 11 '13 13:02

Qadir Hussain


People also ask

How do I parse a JSON file?

If you need to parse a JSON string that returns a dictionary, then you can use the json. loads() method. If you need to parse a JSON file that returns a dictionary, then you can use the json. load() method.

What is JSON parsing in Android Studio?

JSON stands for JavaScript Object Notation.It is an independent data exchange format and is the best alternative for XML. This chapter explains how to parse the JSON file and extract necessary information from it. Android provides four different classes to manipulate JSON data.

What is JSON parse () method?

The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.

What is parsing in android?

Parse is an open-source Android SDK and back-end solution that enables developers to build mobile apps with shared data quickly and without writing any back-end code or custom APIs. Parse is a Node.


1 Answers

you can parse current json String to get value from it as :

// Convert String to json object
JSONObject json = new JSONObject(responseText);

// get LL json object
JSONObject json_LL = json.getJSONObject("LL");

// get value from LL Json Object
String str_value=json_LL.getString("value"); //<< get value here
like image 112
ρяσѕρєя K Avatar answered Sep 28 '22 03:09

ρяσѕρєя K