Skip to content

Instantly share code, notes, and snippets.

@twosouth
Last active June 28, 2018 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twosouth/854616911eea135a7ac73da924bb84c2 to your computer and use it in GitHub Desktop.
Save twosouth/854616911eea135a7ac73da924bb84c2 to your computer and use it in GitHub Desktop.
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