Created
November 2, 2012 10:33
-
-
Save madan712/4000053 to your computer and use it in GitHub Desktop.
Java program to Convert JSONObject to JSON String and vice versa
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | |
} | |
} |
Bahi Convert kar do bhai
Hskcnskdndee
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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"
}