Saturday, 16 March 2019

JsonObject: Construct a json object from string


By passing json string to JSONObject constructor, you can create a json object.

Syntax
JSONObject jsonObject = new JSONObject(jsonString);

Example
String jsonString = "{\"organization\":\"ABC Corportion\",\"name\":\"Krishna\",\"id\":123}";

App.java
package com.sample;

import org.json.JSONObject;

public class App {

 public static void main(String args[]) {
  String jsonString = "{\"organization\":\"ABC Corportion\",\"name\":\"Krishna\",\"id\":123}";

  JSONObject jsonObject = new JSONObject(jsonString);

  System.out.println("name : " + jsonObject.get("name"));
  System.out.println("id : " + jsonObject.get("id"));
  System.out.println("organization : " + jsonObject.get("organization"));

 }
}

Output
name : Krishna
id : 123
organization : ABC Corportion



Previous                                                 Next                                                 Home

No comments:

Post a Comment