{
"firstName": "John",
"lastName": "Smith",
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": 10021
},
"phoneNumbers": [
"212 732-1234",
"646 123-4567"
]
}
Code language: JavaScript (javascript)
import org.json.JSONObject;
...
...
JSONObject json = new JSONObject();
json.put("city", "Mumbai");
json.put("country", "India");
...
String output = json.toString();
...
Code language: Java (java)
Thus by using toString() method you can get the output in JSON format. package net.viralpatel.java;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
public class JsonMain {
public static void main(String[] args) {
Map<String, Long> map = new HashMap<String, Long>();
map.put("A", 10L);
map.put("B", 20L);
map.put("C", 30L);
JSONObject json = new JSONObject();
json.accumulateAll(map);
System.out.println(json.toString());
List<String> list = new ArrayList<String>();
list.add("Sunday");
list.add("Monday");
list.add("Tuesday");
json.accumulate("weekdays", list);
System.out.println(json.toString());
}
}
Code language: Java (java)
Output: {"A":10,"B":20,"C":30}
{"A":10,"B":20,"C":30,"weekdays":["Sunday","Monday","Tuesday"]}
Code language: Java (java)
toJson()
and fromJson()
methods to convert Java objects to JSON and vice-versaimport java.util.List;
import com.google.gson.Gson;
public class Test {
public static void main(String... args) throws Exception {
String json =
"{"
+ "'title': 'Computing and Information systems',"
+ "'id' : 1,"
+ "'children' : 'true',"
+ "'groups' : [{"
+ "'title' : 'Level one CIS',"
+ "'id' : 2,"
+ "'children' : 'true',"
+ "'groups' : [{"
+ "'title' : 'Intro To Computing and Internet',"
+ "'id' : 3,"
+ "'children': 'false',"
+ "'groups':[]"
+ "}]"
+ "}]"
+ "}";
// Now do the magic.
Data data = new Gson().fromJson(json, Data.class);
// Show it.
System.out.println(data);
}
}
class Data {
private String title;
private Long id;
private Boolean children;
private List<Data> groups;
public String getTitle() { return title; }
public Long getId() { return id; }
public Boolean getChildren() { return children; }
public List<Data> getGroups() { return groups; }
public void setTitle(String title) { this.title = title; }
public void setId(Long id) { this.id = id; }
public void setChildren(Boolean children) { this.children = children; }
public void setGroups(List<Data> groups) { this.groups = groups; }
public String toString() {
return String.format("title:%s,id:%d,children:%s,groups:%s", title, id, children, groups);
}
Code language: Java (java)
Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
Hi Team,
I want to know how JSON helps in file upload in Java/Servlet/JSP.
Sample code will be appriciated.
regards,
K. Navin.
Hi Team,
I need to have a simple example of the below
1) Simple JSON example with Servlet
2) Fileupload using JSON & Servlet
Thanks for your help.
Lot of questions left unanswered in this post.. was looking for something more elaborate .. but its ok .. will google it elsewhere
Instead of JSON RPC Jar.. try using this JSON-Lib. It has many advance features compared to library of json.org.
Hi ,
Im trying to write a servlet that returns geoJSON, do I need to use the same library or does anybody have an idea how to convert JTS Geometry directly into JSON?
thanks
Imran
Lahore, Pakistan
can anyone have JSON java tutorial with output?
If anyone has then plz contact me on patil.boss@gmail.com
Hi,
I need to use json object in java and in javascript and in jsp....can any one help me how to populate it on the jsp. ASAP
Hi ,
Hitesh
if you gwill get your answer then please give me too.
Hi,
I need to use json object in java and in javascript and in jsp….can any one help me how to populate it on the jsp. and also I need to have a simple example of the below
1) Simple JSON example with Servlet
Hi,
Please I am interested to get the source code for the example.
Thanks
HI,
i need to work with servlet and json using dropdownlist in html page to retrive the values from database selecting dropdownlist in html page...can u tell me how to do this
Tanks..