-4

I am finding some difficulty in carrying out key values from the json object when my json looks like this:

{
    "access_token":"ya29.UwD44cI4z8KTjCEAAAA1RHrx3vi2zkNnzKvR-lb6o8TtMFZnfuCK71yq59eq4YhdaFsGZcUe2hhUiW7MgHc",
    "token_type":"Bearer",
    "expires_in":3600,
    "refresh_token":"1/qDxXmtxMOTTjhhaSsjfEdMe61Rw9l8I5oS-ct5xNc1o"
}

How do I do that?

J. Steen
  • 15,470
  • 15
  • 56
  • 63
yash
  • 387
  • 1
  • 5
  • 20
  • possible duplicate of [How to parse JSON in Java](http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – Sascha Wolf Jul 31 '14 at 06:27

2 Answers2

1

There are already a lot of examples out there, but HERE you go.

Add this dependency to your project:

<dependency>
      <groupId>net.sf.json-lib</groupId>
      <artifactId>json-lib</artifactId>
      <version>2.3</version>
      <scope>compile</scope>
</dependency>

Put the following JSON sample in your classpath:

{'foo':'bar',
 'coolness':2.0,
 'altitude':39000,
 'pilot':{'firstName':'Buzz',
          'lastName':'Aldrin'},
 'mission':'apollo 11'}

Load the resource from the classpath and parse this JSON as follows:

package com.discursive.answers;
import java.io.InputStream;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import org.apache.commons.io.IOUtils;
public class JsonParsing {
    public static void main(String[] args) throws Exception {
        InputStream is = 
                JsonParsing.class.getResourceAsStream( "sample-json.txt");
        String jsonTxt = IOUtils.toString( is );
        JSONObject json = (JSONObject) JSONSerializer.toJSON( jsonTxt );        
        double coolness = json.getDouble( "coolness" );
        int altitude = json.getInt( "altitude" );
        JSONObject pilot = json.getJSONObject("pilot");
        String firstName = pilot.getString("firstName");
        String lastName = pilot.getString("lastName");
        System.out.println( "Coolness: " + coolness );
        System.out.println( "Altitude: " + altitude );
        System.out.println( "Pilot: " + lastName );
    }
}
romaneso
  • 1,097
  • 1
  • 10
  • 26
0
package stackoverflow.q_25052046;
//Required imports

public class HandleJSON {
    private static String jsonString = "[{ \"access_token\" : \"ya29.UwD44cI4z8KTjCEAAAA1RHrx3vi2zkNnzKvR-lb6o8TtMFZnfuCK71yq59eq4YhdaFsGZcUe2hhUiW7MgHc\", \"token_type\" : \"Bearer\", \"expires_in\" : 3600, \"refresh_token\" : \"1/qDxXmtxMOTTjhhaSsjfEdMe61Rw9l8I5oS-ct5xNc1o\"}]";

    public static void main(String[] args) throws JSONException {
        JSONArray jsonArray = new JSONArray(jsonString);
        System.out.println("Converted object = " + jsonArray); //Outputting the result
        System.out.println("..........................................");

        for (int i = 0; i < jsonArray.length(); i++) { //Iterating over array
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            System.out.println("access_token = " + jsonObject.getString("access_token"));
            System.out.println("token_type = " + jsonObject.getString("token_type"));
            System.out.println("expires_in = " + jsonObject.getString("expires_in"));
            System.out.println("refresh_token = " + jsonObject.getString("refresh_token"));
        }
    }
}

//Output:
//Converted object = [{"expires_in":3600,"token_type":"Bearer","refresh_token":"1/qDxXmtxMOTTjhhaSsjfEdMe61Rw9l8I5oS-ct5xNc1o","access_token":"ya29.UwD44cI4z8KTjCEAAAA1RHrx3vi2zkNnzKvR-lb6o8TtMFZnfuCK71yq59eq4YhdaFsGZcUe2hhUiW7MgHc"}]
//..........................................
//access_token = ya29.UwD44cI4z8KTjCEAAAA1RHrx3vi2zkNnzKvR-lb6o8TtMFZnfuCK71yq59eq4YhdaFsGZcUe2hhUiW7MgHc
//token_type = Bearer
//expires_in = 3600
//refresh_token = 1/qDxXmtxMOTTjhhaSsjfEdMe61Rw9l8I5oS-ct5xNc1o
Nikhil Joshi
  • 817
  • 2
  • 12
  • 34