Skip to content

Instantly share code, notes, and snippets.

@madan712
Created November 2, 2012 10:33
Show Gist options
  • Save madan712/4000053 to your computer and use it in GitHub Desktop.
Save madan712/4000053 to your computer and use it in GitHub Desktop.
Java program to Convert JSONObject to JSON String and vice versa
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class TestJSON {
public static void main(String[] args) {
JSONObject jObject = new JSONObject();
jObject.put("EmployeeId", new Integer(121));
jObject.put("Name", "Ramesh");
jObject.put("Salary", new Double(15000.00));
jObject.put("isPermanent", new Boolean(true));
jObject.put("Nickname", null);
//convert from JSONObject to JSON string
String jsonText = jObject.toJSONString();
System.out.println(jsonText);
JSONParser parser = new JSONParser();
//convert from JSON string to JSONObject
JSONObject newJObject = null;
try {
newJObject = (JSONObject) parser.parse(jsonText);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(newJObject.get("EmployeeId"));
System.out.println(newJObject.get("Name"));
System.out.println(newJObject.get("Salary"));
System.out.println(newJObject.get("isPermanent"));
System.out.println(newJObject.get("Nickname"));
}
}
@bmadhu226
Copy link

How can i pass the folder path in the JSON.

But when passing the path("/latest/object/300000006637803/child/child_c ) to JSON it reading as "/latest/MetaData_c/300000006637803/child/MetaDataFieldsCollection_c"

code:

JSONObject part= new JSONObject();
part=new LinkedHashMap();
part.put("id",count);
part.put("path","/latest/object/300000006637803/child/child_c);
part.put("operation",operation);
System.out.println(part.toJSONString());

output:

{

"id":"part1",

"path":"/latest/MetaData_c/300000006637803/child/MetaDataFieldsCollection_c",

"operation":"create"

}

@7323100178
Copy link

Bahi Convert kar do bhai

@7323100178
Copy link

Hskcnskdndee

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment