-5

I want to create this json using java ,somebody help

{
 "comment": "Check out developer.linkedin.com!",
 "content": {
   "title": "LinkedIn Developers Resources",
   "description": "Leverage LinkedIn's APIs to maximize engagement",
   "submitted-url": "https://developer.linkedin.com",  
"submitted-image-url": "https://example.com/logo.png"
 },
 "visibility": {
  "code": "anyone"
}  
}
lucifer
  • 2,297
  • 18
  • 58
  • 100

2 Answers2

0
  1. Create java class to map to Json..something like

    public class JavaClass  {
    private String comment;
     // other Can be in map or java class for content
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();
    
     //getters and setters
    }
    
  2. populate the object and use any library to convert to Json object.

     JavaClass  object = new JavaClass ();
     object.set(....)
    
  3. Now using JSONbject

     JSONObject jsonObj = new JSONObject( object );
     System.out.println( jsonObj );
    
Sheetal Mohan Sharma
  • 2,908
  • 1
  • 23
  • 24
0
try{
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("comment", "Check out developer.linkedin.com!");
        JSONObject contentJsonObject = new JSONObject();
        contentJsonObject.put("title", "LinkedIn Developers Resources");
        contentJsonObject.put("description", "Leverage LinkedIn's APIs to maximize engagement");
        contentJsonObject.put("submitted-url", "https://developer.linkedin.com");
        contentJsonObject.put("submitted-image-url", "https://example.com/logo.png!");
        jsonObject.put("content",contentJsonObject);
        JSONObject visibilityJsonObject = new JSONObject();
        visibilityJsonObject.put("code", "anyone");
        jsonObject.put("visibility",visibilityJsonObject);
    }catch(JSONException ex){
        Log.i("json ex: ",ex.toString());
    }