How can I convert String to JSON object in Java

+15 votes

I am working on a String. I want to convert it into JSONobject. How can I do this?

Announcement! Career Guide 2019 is out now. Explore careers to become a Big Data Developer or Architect!

Apr 27, 2018 in Java by Daisy
• 8,120 points

edited Apr 12, 2019 by Vardhan 234,470 views

25 answers to this question.

0 votes

object name = new ObjectMapper().readValue(jsonString, object.class);

Hope this helps

answered Nov 13, 2018 by Ali
• 11,340 points
0 votes

//package details

import org.json.simple.JsonObject;

import org.json.simple.Jsoner;

//logic

JsonObject jsonObject = (JsonObject) Jsoner.deserialize(JSON_STRING_VALUE);

answered Jan 7, 2019 by ajay
• 190 points
–3 votes
{
"method" : "conferenceCallout",
"conferenceCallout" :
{
"cli" : "amanda",
"destination" : { "type" : "number", "endpoint" : "+919986351005" },
"domain" : "pstn",
"custom" : "customData",
"locale" : "en-US",
"greeting" : "Welcome to my conference",
"conferenceId" : "myConference",
"enableAce": false,
"enableDice" : false
}
}
answered Jan 28, 2019 by anonymous
0 votes
Gson g = new Gson();
Player p = g.fromJson(jsonString, Player.class)



example of json are

jsonString = {
  "name" : "rajesh",
  "sport" : "cricket",
  "age" : 25,
  "id" : 121,
  "lastScores" : [ 20, 10, 3o, 5, 10, 0, 11, 100 ]
}

 

answered Feb 4, 2019 by rajesh
• 1,230 points
–1 vote
public class HelloWord{

    public static void main(String []args){

       String s = "{\"swagger\\\":\\\"2.0\\\",\\\"paths\\\":{\\\"/upi/ReqPay\\\":{\\\"post\\\":{\\\"parameters\\\":[{\\\"name\\\":\\\"Payload\\\",\\\"description\\\":\\\"Request Body\\\",\\\"required\\\":false,\\\"in\\\":\\\"body\\\",\\\"schema\\\":{\\\"type\\\":\\\"object\\\",\\\"properties\\\":{\\\"payload\\\":{\\\"type\\\":\\\"string\\\"}}}}],\\\"responses\\\":{\\\"200\\\":{\\\"description\\\":\\\"\\\"}},\\\"produces\\\":[\\\"application/xml\\\"],\\\"consumes\\\":[\\\"application/xml\\\"],\\\"x-auth-type\\\":\\\"Application & Application User\\\",\\\"x-throttling-tier\\\":\\\"Unlimited\\\"}}},\\\"info\\\":{\\\"title\\\":\\\"RequestPay\\\",\\\"version\\\":\\\"1.0.0\\\",\\\"description\\\":\\\"API is used for both Direct Pay and Collect Pay transaction\\\\ninitiation by the PSPs and processing the transaction through\\\\none of the following channels IMPS,AEPS etc. \\\"}}\"";

       System.out.println(s.replace("\\"," "));

    }

}
answered Mar 26, 2019 by anonymous
0 votes

example of convert String to JSON object in Java?

jsonstring

jsonString = {
  "name" : "Ronaldo",
  "sport" : "soccer",
  "age" : 25,
  "id" : 121,
  "lastScores" : [ 2, 1, 3, 5, 0, 0, 1, 1 ] 

}

string to json object with using gson

The Gson is an open source library to deal with JSON in Java programs.You can convert JSON String to Java object in just 2 lines by using Gson as shown below

Gson g = new Gson(); 

Player p =g.fromJson(jsonString, Player.class)

You can also convert a Java object to JSON by using to Json() method as shown below

String str = g.toJson(p);

answered Apr 3, 2019 by navdeep
• 330 points
I as well as my pals ended up viewing the good thoughts from your site then quickly got an awful feeling I never expressed respect to the web blog owner for those tips. All of the young men appeared to be for that reason excited to study all of them and have honestly been loving these things. Thank you for indeed being well accommodating and for pick out such incredible things millions of individuals are really wanting to learn about. Our honest regret for not expressing appreciation to  earlier.
hermes birkin http://www.hermesbirkinbags.us.com
0 votes
{
  "total_count": 513,
  "incomplete_results": false,
  "items": [
    {
      "login": "moyheen",
      "id": 8110201,
      "node_id": "MDQ6VXNlcjgxMTAyMDE=",
      "avatar_url": "https://avatars2.githubusercontent.com/u/8110201?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/moyheen",
      "html_url": "https://github.com/moyheen",
      "followers_url": "https://api.github.com/users/moyheen/followers",
      "following_url": "https://api.github.com/users/moyheen/following{/other_user}",
      "gists_url": "https://api.github.com/users/moyheen/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/moyheen/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/moyheen/subscriptions",
      "organizations_url": "https://api.github.com/users/moyheen/orgs",
      "repos_url": "https://api.github.com/users/moyheen/repos",
      "events_url": "https://api.github.com/users/moyheen/events{/privacy}",
      "received_events_url": "https://api.github.com/users/moyheen/received_events",
      "type": "User",
      "site_admin": false,
      "score": 1.0
    },
    {
      "login": "o-kamiye",
      "id": 4929406,
      "node_id": "MDQ6VXNlcjQ5Mjk0MDY=",
      "avatar_url": "https://avatars1.githubusercontent.com/u/4929406?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/o-kamiye",
      "html_url": "https://github.com/o-kamiye",
      "followers_url": "https://api.github.com/users/o-kamiye/followers",
      "following_url": "https://api.github.com/users/o-kamiye/following{/other_user}",
      "gists_url": "https://api.github.com/users/o-kamiye/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/o-kamiye/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/o-kamiye/subscriptions",
      "organizations_url": "https://api.github.com/users/o-kamiye/orgs",
      "repos_url": "https://api.github.com/users/o-kamiye/repos",
      "events_url": "https://api.github.com/users/o-kamiye/events{/privacy}",
      "received_events_url": "https://api.github.com/users/o-kamiye/received_events",
      "type": "User",
      "site_admin": false,
      "score": 1.0
    },
    {
      "login": "aliumujib",
      "id": 13552664,
      "node_id": "MDQ6VXNlcjEzNTUyNjY0",
      "avatar_url": "https://avatars3.githubusercontent.com/u/13552664?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/aliumujib",
      "html_url": "https://github.com/aliumujib",
      "followers_url": "https://api.github.com/users/aliumujib/followers",
      "following_url": "https://api.github.com/users/aliumujib/following{/other_user}",
      "gists_url": "https://api.github.com/users/aliumujib/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/aliumujib/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/aliumujib/subscriptions",
      "organizations_url": "https://api.github.com/users/aliumujib/orgs",
      "repos_url": "https://api.github.com/users/aliumujib/repos",
      "events_url": "https://api.github.com/users/aliumujib/events{/privacy}",
      "received_events_url": "https://api.github.com/users/aliumujib/received_events",
      "type": "User",
      "site_admin": false,
      "score": 1.0
    },
    {
      "login": "biodunalfet",
      "id": 6963510,
      "node_id": "MDQ6VXNlcjY5NjM1MTA=",
      "avatar_url": "https://avatars1.githubusercontent.com/u/6963510?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/biodunalfet",
      "html_url": "https://github.com/biodunalfet",
      "followers_url": "https://api.github.com/users/biodunalfet/followers",
      "following_url": "https://api.github.com/users/biodunalfet/following{/other_user}",
      "gists_url": "https://api.github.com/users/biodunalfet/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/biodunalfet/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/biodunalfet/subscriptions",
      "organizations_url": "https://api.github.com/users/biodunalfet/orgs",
      "repos_url": "https://api.github.com/users/biodunalfet/repos",
      "events_url": "https://api.github.com/users/biodunalfet/events{/privacy}",
      "received_events_url": "https://api.github.com/users/biodunalfet/received_events",
      "type": "User",
      "site_admin": false,
      "score": 1.0
    },
    {
      "login": "helios66",
      "id": 678974,
      "node_id": "MDQ6VXNlcjY3ODk3NA==",
      "avatar_url": "https://avatars3.githubusercontent.com/u/678974?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/helios66",
      "html_url": "https://github.com/helios66",
      "followers_url": "https://api.github.com/users/helios66/followers",
      "following_url": "https://api.github.com/users/helios66/following{/other_user}",
      "gists_url": "https://api.github.com/users/helios66/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/helios66/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/helios66/subscriptions",
      "organizations_url": "https://api.github.com/users/helios66/orgs",
      "repos_url": "https://api.github.com/users/helios66/repos",
      "events_url": "https://api.github.com/users/helios66/events{/privacy}",
      "received_events_url": "https://api.github.com/users/helios66/received_events",
      "type": "User",
      "site_admin": false,
      "score": 1.0
    },
    {
      "login": "tundeojediran",
      "id": 6113672,
      "node_id": "MDQ6VXNlcjYxMTM2NzI=",
      "avatar_url": "https://avatars0.githubusercontent.com/u/6113672?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/tundeojediran",
      "html_url": "https://github.com/tundeojediran",
      "followers_url": "https://api.github.com/users/tundeojediran/followers",
      "following_url": "https://api.github.com/users/tundeojediran/following{/other_user}",
      "gists_url": "https://api.github.com/users/tundeojediran/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/tundeojediran/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/tundeojediran/subscriptions",
      "organizations_url": "https://api.github.com/users/tundeojediran/orgs",
      "repos_url": "https://api.github.com/users/tundeojediran/repos",
      "events_url": "https://api.github.com/users/tundeojediran/events{/privacy}",
      "received_events_url": "https://api.github.com/users/tundeojediran/received_events",
      "type": "User",
      "site_admin": false,
      "score": 1.0
    },
    {
      "login": "KingsMentor",
      "id": 5605785,
      "node_id": "MDQ6VXNlcjU2MDU3ODU=",
      "avatar_url": "https://avatars1.githubusercontent.com/u/5605785?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/KingsMentor",
      "html_url": "https://github.com/KingsMentor",
      "followers_url": "https://api.github.com/users/KingsMentor/followers",
      "following_url": "https://api.github.com/users/KingsMentor/following{/other_user}",
      "gists_url": "https://api.github.com/users/KingsMentor/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/KingsMentor/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/KingsMentor/subscriptions",
      "organizations_url": "https://api.github.com/users/KingsMentor/orgs",
      "repos_url": "https://api.github.com/users/KingsMentor/repos",
      "events_url": "https://api.github.com/users/KingsMentor/events{/privacy}",
      "received_events_url": "https://api.github.com/users/KingsMentor/received_events",
      "type": "User",
      "site_admin": false,
      "score": 1.0
    },
    {
      "login": "adetuyiTolu",
      "id": 19608533,
      "node_id": "MDQ6VXNlcjE5NjA4NTMz",
      "avatar_url": "https://avatars1.githubusercontent.com/u/19608533?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/adetuyiTolu",
      "html_url": "https://github.com/adetuyiTolu",
      "followers_url": "https://api.github.com/users/adetuyiTolu/followers",
      "following_url": "https://api.github.com/users/adetuyiTolu/following{/other_user}",
      "gists_url": "https://api.github.com/users/adetuyiTolu/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/adetuyiTolu/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/adetuyiTolu/subscriptions",
      "organizations_url": "https://api.github.com/users/adetuyiTolu/orgs",
      "repos_url": "https://api.github.com/users/adetuyiTolu/repos",
      "events_url": "https://api.github.com/users/adetuyiTolu/events{/privacy}",
      "received_events_url": "https://api.github.com/users/adetuyiTolu/received_events",
      "type": "User",
      "site_admin": false,
      "score": 1.0
    },
    {
      "login": "kizitonwose",
      "id": 15170090,
      "node_id": "MDQ6VXNlcjE1MTcwMDkw",
      "avatar_url": "https://avatars2.githubusercontent.com/u/15170090?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/kizitonwose",
      "html_url": "https://github.com/kizitonwose",
      "followers_url": "https://api.github.com/users/kizitonwose/followers",
      "following_url": "https://api.github.com/users/kizitonwose/following{/other_user}",
      "gists_url": "https://api.github.com/users/kizitonwose/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/kizitonwose/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/kizitonwose/subscriptions",
      "organizations_url": "https://api.github.com/users/kizitonwose/orgs",
      "repos_url": "https://api.github.com/users/kizitonwose/repos",
      "events_url": "https://api.github.com/users/kizitonwose/events{/privacy}",
      "received_events_url": "https://api.github.com/users/kizitonwose/received_events",
      "type": "User",
      "site_admin": false,
      "score": 1.0
    },
    {
      "login": "emmanuelkehinde",
      "id": 16584163,
      "node_id": "MDQ6VXNlcjE2NTg0MTYz",
      "avatar_url": "https://avatars3.githubusercontent.com/u/16584163?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/emmanuelkehinde",
      "html_url": "https://github.com/emmanuelkehinde",
      "followers_url": "https://api.github.com/users/emmanuelkehinde/followers",
      "following_url": "https://api.github.com/users/emmanuelkehinde/following{/other_user}",
      "gists_url": "https://api.github.com/users/emmanuelkehinde/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/emmanuelkehinde/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/emmanuelkehinde/subscriptions",
      "organizations_url": "https://api.github.com/users/emmanuelkehinde/orgs",
      "repos_url": "https://api.github.com/users/emmanuelkehinde/repos",
      "events_url": "https://api.github.com/users/emmanuelkehinde/events{/privacy}",
      "received_events_url": "https://api.github.com/users/emmanuelkehinde/received_events",
      "type": "User",
      "site_admin": false,
      "score": 1.0
    },
    {
      "login": "SeunAdelekan",
      "id": 16557263,
      "node_id": "MDQ6VXNlcjE2NTU3MjYz",
      "avatar_url": "https://avatars2.githubusercontent.com/u/16557263?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/SeunAdelekan",
      "html_url": "https://github.com/SeunAdelekan",
      "followers_url": "https://api.github.com/users/SeunAdelekan/followers",
      "following_url": "https://api.github.com/users/SeunAdelekan/following{/other_user}",
      "gists_url": "https://api.github.com/users/SeunAdelekan/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/SeunAdelekan/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/SeunAdelekan/subscriptions",
      "organizations_url": "https://api.github.com/users/SeunAdelekan/orgs",
      "repos_url": "https://api.github.com/users/SeunAdelekan/repos",
      "events_url": "https://api.github.com/users/SeunAdelekan/events{/privacy}",
      "received_events_url": "https://api.github.com/users/SeunAdelekan/received_events",
      "type": "User",
      "site_admin": false,
      "score": 1.0
    },
    {
      "login": "cornzyblack",
      "id": 5692718,
      "node_id": "MDQ6VXNlcjU2OTI3MTg=",
      "avatar_url": "https://avatars2.githubusercontent.com/u/5692718?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/cornzyblack",
      "html_url": "https://github.com/cornzyblack",
      "followers_url": "https://api.github.com/users/cornzyblack/followers",
      "following_url": "https://api.github.com/users/cornzyblack/following{/other_user}",
      "gists_url": "https://api.github.com/users/cornzyblack/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/cornzyblack/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/cornzyblack/subscriptions",
      "organizations_url": "https://api.github.com/users/cornzyblack/orgs",
      "repos_url": "https://api.github.com/users/cornzyblack/repos",
      "events_url": "https://api.github.com/users/cornzyblack/events{/privacy}",
      "received_events_url": "https://api.github.com/users/cornzyblack/received_events",
      "type": "User",
      "site_admin": false,
      "score": 1.0
    },
    {
      "login": "timi-codes",
      "id": 7896429,
      "node_id": "MDQ6VXNlcjc4OTY0Mjk=",
      "avatar_url": "https://avatars1.githubusercontent.com/u/7896429?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/timi-codes",
      "html_url": "https://github.com/timi-codes",
      "followers_url": "https://api.github.com/users/timi-codes/followers",
      "following_url": "https://api.github.com/users/timi-codes/following{/other_user}",
      "gists_url": "https://api.github.com/users/timi-codes/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/timi-codes/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/timi-codes/subscriptions",
      "organizations_url": "https://api.github.com/users/timi-codes/orgs",
      "repos_url": "https://api.github.com/users/timi-codes/repos",
      "events_url": "https://api.github.com/users/timi-codes/events{/privacy}",
      "received_events_url": "https://api.github.com/users/timi-codes/received_events",
      "type": "User",
      "site_admin": false,
      "score": 1.0
    },
    {
      "login": "chalu",
      "id": 276286,
      "node_id": "MDQ6VXNlcjI3NjI4Ng==",
      "avatar_url": "https://avatars0.githubusercontent.com/u/276286?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/chalu",
      "html_url": "https://github.com/chalu",
      "followers_url": "https://api.github.com/users/chalu/followers",
      "following_url": "https://api.github.com/users/chalu/following{/other_user}",
      "gists_url": "https://api.github.com/users/chalu/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/chalu/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/chalu/subscriptions",
      "organizations_url": "https://api.github.com/users/chalu/orgs",
      "repos_url": "https://api.github.com/users/chalu/repos",
      "events_url": "https://api.github.com/users/chalu/events{/privacy}",
      "received_events_url": "https://api.github.com/users/chalu/received_events",
      "type": "User",
      "site_admin": false,
      "score": 1.0
    },
    {
      "login": "yomiolatunji",
      "id": 5986979,
      "node_id": "MDQ6VXNlcjU5ODY5Nzk=",
      "avatar_url": "https://avatars2.githubusercontent.com/u/5986979?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/yomiolatunji",
      "html_url": "https://github.com/yomiolatunji",
      "followers_url": "https://api.github.com/users/yomiolatunji/followers",
      "following_url": "https://api.github.com/users/yomiolatunji/following{/other_user}",
      "gists_url": "https://api.github.com/users/yomiolatunji/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/yomiolatunji/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/yomiolatunji/subscriptions",
      "organizations_url": "https://api.github.com/users/yomiolatunji/orgs",
      "repos_url": "https://api.github.com/users/yomiolatunji/repos",
      "events_url": "https://api.github.com/users/yomiolatunji/events{/privacy}",
      "received_events_url": "https://api.github.com/users/yomiolatunji/received_events",
      "type": "User",
      "site_admin": false,
      "score": 1.0
    },
    {
      "login": "larikraun",
      "id": 2240178,
      "node_id": "MDQ6VXNlcjIyNDAxNzg=",
      "avatar_url": "https://avatars2.githubusercontent.com/u/2240178?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/larikraun",
      "html_url": "https://github.com/larikraun",
      "followers_url": "https://api.github.com/users/larikraun/followers",
      "following_url": "https://api.github.com/users/larikraun/following{/other_user}",
      "gists_url": "https://api.github.com/users/larikraun/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/larikraun/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/larikraun/subscriptions",
      "organizations_url": "https://api.github.com/users/larikraun/orgs",
      "repos_url": "https://api.github.com/users/larikraun/repos",
      "events_url": "https://api.github.com/users/larikraun/events{/privacy}",
      "received_events_url": "https://api.github.com/users/larikraun/received_events",
      "type": "User",
      "site_admin": false,
      "score": 1.0
    },
    {
      "login": "AdoraNwodo",
      "id": 13754478,
      "node_id": "MDQ6VXNlcjEzNzU0NDc4",
      "avatar_url": "https://avatars0.githubusercontent.com/u/13754478?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/AdoraNwodo",
      "html_url": "https://github.com/AdoraNwodo",
      "followers_url": "https://api.github.com/users/AdoraNwodo/followers",
      "following_url": "https://api.github.com/users/AdoraNwodo/following{/other_user}",
      "gists_url": "https://api.github.com/users/AdoraNwodo/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/AdoraNwodo/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/AdoraNwodo/subscriptions",
      "organizations_url": "https://api.github.com/users/AdoraNwodo/orgs",
      "repos_url": "https://api.github.com/users/AdoraNwodo/repos",
      "events_url": "https://api.github.com/users/AdoraNwodo/events{/privacy}",
      "received_events_url": "https://api.github.com/users/AdoraNwodo/received_events",
      "type": "User",
      "site_admin": false,
      "score": 1.0
    },
    {
      "login": "TaslimOseni",
      "id": 19727515,
      "node_id": "MDQ6VXNlcjE5NzI3NTE1",
      "avatar_url": "https://avatars2.githubusercontent.com/u/19727515?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/TaslimOseni",
      "html_url": "https://github.com/TaslimOseni",
      "followers_url": "https://api.github.com/users/TaslimOseni/followers",
      "following_url": "https://api.github.com/users/TaslimOseni/following{/other_user}",
      "gists_url": "https://api.github.com/users/TaslimOseni/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/TaslimOseni/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/TaslimOseni/subscriptions",
      "organizations_url": "https://api.github.com/users/TaslimOseni/orgs",
      "repos_url": "https://api.github.com/users/TaslimOseni/repos",
      "events_url": "https://api.github.com/users/TaslimOseni/events{/privacy}",
      "received_events_url": "https://api.github.com/users/TaslimOseni/received_events",
      "type": "User",
      "site_admin": false,
      "score": 1.0
    },
    {
      "login": "chikecodes",
      "id": 9150660,
      "node_id": "MDQ6VXNlcjkxNTA2NjA=",
      "avatar_url": "https://avatars0.githubusercontent.com/u/9150660?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/chikecodes",
      "html_url": "https://github.com/chikecodes",
      "followers_url": "https://api.github.com/users/chikecodes/followers",
      "following_url": "https://api.github.com/users/chikecodes/following{/other_user}",
      "gists_url": "https://api.github.com/users/chikecodes/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/chikecodes/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/chikecodes/subscriptions",
      "organizations_url": "https://api.github.com/users/chikecodes/orgs",
      "repos_url": "https://api.github.com/users/chikecodes/repos",
      "events_url": "https://api.github.com/users/chikecodes/events{/privacy}",
      "received_events_url": "https://api.github.com/users/chikecodes/received_events",
      "type": "User",
      "site_admin": false,
      "score": 1.0
    },
    {
      "login": "IEnoobong",
      "id": 7225217,
      "node_id": "MDQ6VXNlcjcyMjUyMTc=",
      "avatar_url": "https://avatars3.githubusercontent.com/u/7225217?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/IEnoobong",
      "html_url": "https://github.com/IEnoobong",
      "followers_url": "https://api.github.com/users/IEnoobong/followers",
      "following_url": "https://api.github.com/users/IEnoobong/following{/other_user}",
      "gists_url": "https://api.github.com/users/IEnoobong/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/IEnoobong/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/IEnoobong/subscriptions",
      "organizations_url": "https://api.github.com/users/IEnoobong/orgs",
      "repos_url": "https://api.github.com/users/IEnoobong/repos",
      "events_url": "https://api.github.com/users/IEnoobong/events{/privacy}",
      "received_events_url": "https://api.github.com/users/IEnoobong/received_events",
      "type": "User",
      "site_admin": false,
      "score": 1.0
    },
    {
      "login": "androidmaven",
      "id": 21104847,
      "node_id": "MDQ6VXNlcjIxMTA0ODQ3",
      "avatar_url": "https://avatars2.githubusercontent.com/u/21104847?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/androidmaven",
      "html_url": "https://github.com/androidmaven",
      "followers_url": "https://api.github.com/users/androidmaven/followers",
      "following_url": "https://api.github.com/users/androidmaven/following{/other_user}",
      "gists_url": "https://api.github.com/users/androidmaven/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/androidmaven/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/androidmaven/subscriptions",
      "organizations_url": "https://api.github.com/users/androidmaven/orgs",
      "repos_url": "https://api.github.com/users/androidmaven/repos",
      "events_url": "https://api.github.com/users/androidmaven/events{/privacy}",
      "received_events_url": "https://api.github.com/users/androidmaven/received_events",
      "type": "User",
      "site_admin": false,
      "score": 1.0
    },
    {
      "login": "Kennypee",
      "id": 20874031,
      "node_id": "MDQ6VXNlcjIwODc0MDMx",
      "avatar_url": "https://avatars2.githubusercontent.com/u/20874031?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/Kennypee",
      "html_url": "https://github.com/Kennypee",
      "followers_url": "https://api.github.com/users/Kennypee/followers",
      "following_url": "https://api.github.com/users/Kennypee/following{/other_user}",
      "gists_url": "https://api.github.com/users/Kennypee/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/Kennypee/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/Kennypee/subscriptions",
      "organizations_url": "https://api.github.com/users/Kennypee/orgs",
      "repos_url": "https://api.github.com/users/Kennypee/repos",
      "events_url": "https://api.github.com/users/Kennypee/events{/privacy}",
      "received_events_url": "https://api.github.com/users/Kennypee/received_events",
      "type": "User",
      "site_admin": false,
      "score": 1.0
    },
    {
      "login": "ajibigad",
      "id": 4974988,
      "node_id": "MDQ6VXNlcjQ5NzQ5ODg=",
      "avatar_url": "https://avatars3.githubusercontent.com/u/4974988?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/ajibigad",
      "html_url": "https://github.com/ajibigad",
      "followers_url": "https://api.github.com/users/ajibigad/followers",
      "following_url": "https://api.github.com/users/ajibigad/following{/other_user}",
      "gists_url": "https://api.github.com/users/ajibigad/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/ajibigad/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/ajibigad/subscriptions",
      "organizations_url": "https://api.github.com/users/ajibigad/orgs",
      "repos_url": "https://api.github.com/users/ajibigad/repos",
      "events_url": "https://api.github.com/users/ajibigad/events{/privacy}",
      "received_events_url": "https://api.github.com/users/ajibigad/received_events",
      "type": "User",
      "site_admin": false,
      "score": 1.0
    },
    {
      "login": "Elvis10ten",
      "id": 4107815,
      "node_id": "MDQ6VXNlcjQxMDc4MTU=",
      "avatar_url": "https://avatars0.githubusercontent.com/u/4107815?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/Elvis10ten",
      "html_url": "https://github.com/Elvis10ten",
      "followers_url": "https://api.github.com/users/Elvis10ten/followers",
      "following_url": "https://api.github.com/users/Elvis10ten/following{/other_user}",
      "gists_url": "https://api.github.com/users/Elvis10ten/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/Elvis10ten/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/Elvis10ten/subscriptions",
      "organizations_url": "https://api.github.com/users/Elvis10ten/orgs",
      "repos_url": "https://api.github.com/users/Elvis10ten/repos",
      "events_url": "https://api.github.com/users/Elvis10ten/events{/privacy}",
      "received_events_url": "https://api.github.com/users/Elvis10ten/received_events",
      "type": "User",
      "site_admin": false,
      "score": 1.0
    },
    {
      "login": "Ezike",
      "id": 17779130,
      "node_id": "MDQ6VXNlcjE3Nzc5MTMw",
      "avatar_url": "https://avatars1.githubusercontent.com/u/17779130?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/Ezike",
      "html_url": "https://github.com/Ezike",
      "followers_url": "https://api.github.com/users/Ezike/followers",
      "following_url": "https://api.github.com/users/Ezike/following{/other_user}",
      "gists_url": "https://api.github.com/users/Ezike/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/Ezike/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/Ezike/subscriptions",
      "organizations_url": "https://api.github.com/users/Ezike/orgs",
      "repos_url": "https://api.github.com/users/Ezike/repos",
      "events_url": "https://api.github.com/users/Ezike/events{/privacy}",
      "received_events_url": "https://api.github.com/users/Ezike/received_events",
      "type": "User",
      "site_admin": false,
      "score": 1.0
    },
    {
      "login": "tdscientist",
      "id": 6681855,
      "node_id": "MDQ6VXNlcjY2ODE4NTU=",
      "avatar_url": "https://avatars1.githubusercontent.com/u/6681855?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/tdscientist",
      "html_url": "https://github.com/tdscientist",
      "followers_url": "https://api.github.com/users/tdscientist/followers",
      "following_url": "https://api.github.com/users/tdscientist/following{/other_user}",
      "gists_url": "https://api.github.com/users/tdscientist/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/tdscientist/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/tdscientist/subscriptions",
      "organizations_url": "https://api.github.com/users/tdscientist/orgs",
      "repos_url": "https://api.github.com/users/tdscientist/repos",
      "events_url": "https://api.github.com/users/tdscientist/events{/privacy}",
      "received_events_url": "https://api.github.com/users/tdscientist/received_events",
      "type": "User",
      "site_admin": false,
      "score": 1.0
    },
    {
      "login": "AyoGitNg",
      "id": 13318431,
      "node_id": "MDQ6VXNlcjEzMzE4NDMx",
      "avatar_url": "https://avatars2.githubusercontent.com/u/13318431?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/AyoGitNg",
      "html_url": "https://github.com/AyoGitNg",
      "followers_url": "https://api.github.com/users/AyoGitNg/followers",
      "following_url": "https://api.github.com/users/AyoGitNg/following{/other_user}",
      "gists_url": "https://api.github.com/users/AyoGitNg/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/AyoGitNg/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/AyoGitNg/subscriptions",
      "organizations_url": "https://api.github.com/users/AyoGitNg/orgs",
      "repos_url": "https://api.github.com/users/AyoGitNg/repos",
      "events_url": "https://api.github.com/users/AyoGitNg/events{/privacy}",
      "received_events_url": "https://api.github.com/users/AyoGitNg/received_events",
      "type": "User",
      "site_admin": false,
      "score": 1.0
    },
    {
      "login": "ftchirou",
      "id": 4760130,
      "node_id": "MDQ6VXNlcjQ3NjAxMzA=",
      "avatar_url": "https://avatars0.githubusercontent.com/u/4760130?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/ftchirou",
      "html_url": "https://github.com/ftchirou",
      "followers_url": "https://api.github.com/users/ftchirou/followers",
      "following_url": "https://api.github.com/users/ftchirou/following{/other_user}",
      "gists_url": "https://api.github.com/users/ftchirou/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/ftchirou/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/ftchirou/subscriptions",
      "organizations_url": "https://api.github.com/users/ftchirou/orgs",
      "repos_url": "https://api.github.com/users/ftchirou/repos",
      "events_url": "https://api.github.com/users/ftchirou/events{/privacy}",
      "received_events_url": "https://api.github.com/users/ftchirou/received_events",
      "type": "User",
      "site_admin": false,
      "score": 1.0
    },
    {
      "login": "FeezyHendrix",
      "id": 27918971,
      "node_id": "MDQ6VXNlcjI3OTE4OTcx",
      "avatar_url": "https://avatars3.githubusercontent.com/u/27918971?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/FeezyHendrix",
      "html_url": "https://github.com/FeezyHendrix",
      "followers_url": "https://api.github.com/users/FeezyHendrix/followers",
      "following_url": "https://api.github.com/users/FeezyHendrix/following{/other_user}",
      "gists_url": "https://api.github.com/users/FeezyHendrix/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/FeezyHendrix/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/FeezyHendrix/subscriptions",
      "organizations_url": "https://api.github.com/users/FeezyHendrix/orgs",
      "repos_url": "https://api.github.com/users/FeezyHendrix/repos",
      "events_url": "https://api.github.com/users/FeezyHendrix/events{/privacy}",
      "received_events_url": "https://api.github.com/users/FeezyHendrix/received_events",
      "type": "User",
      "site_admin": false,
      "score": 1.0
    },
    {
      "login": "tobydigz",
      "id": 15001016,
      "node_id": "MDQ6VXNlcjE1MDAxMDE2",
      "avatar_url": "https://avatars2.githubusercontent.com/u/15001016?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/tobydigz",
      "html_url": "https://github.com/tobydigz",
      "followers_url": "https://api.github.com/users/tobydigz/followers",
      "following_url": "https://api.github.com/users/tobydigz/following{/other_user}",
      "gists_url": "https://api.github.com/users/tobydigz/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/tobydigz/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/tobydigz/subscriptions",
      "organizations_url": "https://api.github.com/users/tobydigz/orgs",
      "repos_url": "https://api.github.com/users/tobydigz/repos",
      "events_url": "https://api.github.com/users/tobydigz/events{/privacy}",
      "received_events_url": "https://api.github.com/users/tobydigz/received_events",
      "type": "User",
      "site_admin": false,
      "score": 1.0
    }
  ]
}
answered Apr 14, 2019 by anonymous
What about this?
0 votes
There are lots of tools for this....
answered Jun 27, 2019 by Helium
Which tools can be used??
+2 votes
String prams="{"field1":"value","field2":"value2" }";

JSONObject json = new JSONObject(prams); // just this line is enough

make sure the String is alredy in proper json format as shown above
answered Jan 27, 2020 by Edd
Thanks, @Edd for your contribution.

Please register at Edureka Community and earn credits for every contribution. A contribution could be asking a question, answering, commenting or even upvoting/downvoting an answer or question.

These credits can be used to get a discount on the course. Also, you could become the admin at Edureka Community with certain points.

Cheers!
0 votes
you can use google gson.
answered Jul 12, 2020 by anonymous
• 140 points
0 votes
Here, you can use the content of Google's gson, serialization, and deserialization.
answered Jul 24, 2020 by GurwinderKaur
• 380 points
0 votes

String to JSON Object using Gson

The Gson is an open-source library to deal with JSON in Java programs. It comes from none other than Google, which is also behind Guava, a common purpose library for Java programmers. You can convert JSON String to Java object in just 2 lines by using Gson as shown below :
 

Gson g = new Gson();
Player p = g.fromJson(jsonString, Player.class)


You can also convert a Java object to JSON by using toJson() method as shown below
 

String str = g.toJson(p);


The good thing about Gson is that it's feature-rich and comes from Google, which is known for performance.

 

answered Aug 29, 2020 by Pistle
• 1,000 points
+1 vote

Another approach could be:

In the following program, we have converted a JSON String to JSON Object.

JsonStringToJsonObjectExample.java
  1. import org.json.JSONObject;  
  2. import org.json.JSONArray;  
  3. public class JsonStringToJsonObjectExample   
  4. {  
  5. public static void main(String args[])   
  6. {  
  7. String str = "[{\"No\":\"17\",\"Name\":\"Andrew\"},{\"No\":\"18\",\"Name\":\"Peter\"}, {\"No\":\"19\",\"Name\":\"Tom\"}]";  
  8. JSONArray array = new JSONArray(str);  
  9. for(int i=0; i < array.length(); i++)   
  10. {  
  11. JSONObject object = array.getJSONObject(i);  
  12. System.out.println(object.getString("No"));  
  13. System.out.println(object.getString("Name"));  
  14. }  
  15. }  
  16. }  
Output:

17
Andrew
18
Peter
19
Tom

Let's see another example.

JsonStringToJsonObjectExample2.java

  1. import org.json.*;  
  2. public class JsonStringToJsonObjectExample2  
  3. {  
  4. public static void main(String[] args)   
  5. {  
  6. String string = "{\"name\": \"Sam Smith\", \"technology\": \"Python\"}";  
  7. JSONObject json = new JSONObject(string);  
  8. System.out.println(json.toString());  
  9. String technology = json.getString("technology");  
  10. System.out.println(technology);  
  11. }  
  12. }  
Output:

Sam Smith
Python

answered Dec 10, 2020 by Gitika
• 65,850 points
0 votes

Using org.json library:

try {
     JSONObject jsonObject = new JSONObject("{\"phonetype\":\"N95\",\"cat\":\"WP\"}");
}catch (JSONException err){
     Log.d("Error", err.toString());
}
answered Dec 10, 2020 by Rajiv
• 8,910 points
0 votes

you to create JSON object from JSON formatted String e.g. Gson from Google, Jackson, and json-simple. In this tutorial, you will learn how to use these 3 main libraries to do this conversion with step by step examples.

Even though you can use a simple or complex JSON String like with lots of attributes and JSON arrays, I'll use following JSON String for example purpose:

jsonString = {
  "name" : "Ronaldo",
  "sport" : "soccer",
  "age" : 25,
  "id" : 121,
  "lastScores" : [ 2, 1, 3, 5, 0, 0, 1, 1 ]
}

It's simple, has 5 attributes, two of which are String and the other two are numeric. One attribute, lastScore is a JSON array.
 

String to JSON Object using Gson

The Gson is an open-source library to deal with JSON in Java programs. It comes from none other than Google, which is also behind Guava, a common purpose library for Java programmers. You can convert JSON String to Java object in just 2 lines by using Gson as shown below :
 

Gson g = new Gson();
Player p = g.fromJson(jsonString, Player.class)


You can also convert a Java object to JSON by using toJson() method as shown below
 

String str = g.toJson(p);

JSON String to Java object using JSON-Simple

The JSON-Simple is another open-source library which supports JSON parsing and formatting. The good thing about this library is its small size, which is perfect for memory constraint environments like J2ME and Android.
 

JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(stringToParse);


The good thing about json-simple is that it is also JDK 1.2 compatible, which means you can use it on a legacy project which is not yet in Java 5.


 

answered Dec 10, 2020 by Roshni
• 10,500 points

Related Questions In Java

0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 59,170 points
1,053 views
0 votes
2 answers
0 votes
1 answer

how to read csv file form sftp connection and store into string object in java code and convert into json.....post it using rest api

Hey, @Pooja, Before starting with anything you should ...READ MORE

answered May 13, 2020 in Java by Roshni
• 10,500 points
2,073 views
0 votes
2 answers

How can I convert byte array into hex string in Java?

public static String byteArrayToHex(byte[] a) { ...READ MORE

answered Aug 29, 2019 in Java by Sirajul
• 59,170 points
1,920 views
0 votes
2 answers

How an object array can be converted to string array in java?

System.arraycopy is the most efficient way, but ...READ MORE

answered Aug 8, 2018 in Java by Sushmita
• 6,900 points
2,820 views
0 votes
2 answers

How do I convert a String to an int in Java?

Use the lines of code mentioned below:- String ...READ MORE

answered Feb 9 in Java by Soham
• 8,730 points
393 views
0 votes
2 answers

How to convert a JSON String into Object in Java?

You could probably check out Google's Gson: ...READ MORE

answered Aug 21, 2019 in Java by Sirajul
• 59,170 points
2,362 views
0 votes
1 answer

How to convert or cast hashmap to JSON object in Java, and again convert JSON object to JSON string?

You can use: new JSONObject(map); READ MORE

answered Jun 27, 2018 in Java by Akrati
• 3,190 points
5,424 views
0 votes
1 answer

How can we return a JSON object from a Java Servlet?

response.setContentType("application/json"); // Get the printwriter object from response ...READ MORE

answered Jul 6, 2018 in Java by sharth
• 3,350 points
4,915 views
0 votes
2 answers

How do we convert JSON to Map in Java

When you don't know structure of json. ...READ MORE

answered Oct 31, 2018 in Java by Sushmita
• 6,900 points
4,200 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP