Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple example of creating a JSON object
package mcp.json.sample;
import org.json.simple.JSONObject;
public class MCPSimpleJSON_1 {
public static void main(String[] args) {
MCPSimpleJSON_1 msj = new MCPSimpleJSON_1();
System.out.println(msj.getJson());
}
@SuppressWarnings("unchecked")
public String getJson() {
// get the JSON api here: https://github.com/fangyidong/json-simple
// create this JSON block with the java below
// {
// "success": true,
// "firstName": "Scott",
// "lastName": "Salisbury",
// "zipCode": "37067",
// "address2": "Apt B",
// "city": "Franklin",
// "address1": "10 Main St",
// "state": "TN"
// }
JSONObject jo = new JSONObject(); // working JSONObject
jo.put("success",true);
jo.put("firstName", "Scott");
jo.put("lastName", "Salisbury");
jo.put("address1", "10 Main St");
jo.put("address2", "Apt B");
jo.put("city", "Franklin");
jo.put("state", "TN");
jo.put("zipCode", "37067");
return jo.toString(); // return the created JSONObject as a string
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment