I am using json-rpc-1.0.jar.Below is my code. I need to convert InputStream object into JSON since the response is in JSON.
I did verify the json response obtained from Zappos API. It is valid.
PrintWriter out = resp.getWriter();
String jsonString = null;
URL url = new URL("http://api.zappos.com/Search?term=boots&key=my_key");
InputStream inputStream = url.openConnection().getInputStream();
resp.setContentType("application/json");
JSONSerializer jsonSerializer = new JSONSerializer();
try {
jsonString = jsonSerializer.toJSON(inputStream);
} catch (MarshallException e) {
e.printStackTrace();
}
out.print(jsonString);
I get the below mentioned exception:
com.metaparadigm.jsonrpc.MarshallException: can't marshall sun.net.www.protocol.http.HttpURLConnection$HttpInputStream
at com.metaparadigm.jsonrpc.JSONSerializer.marshall(JSONSerializer.java:251)
at com.metaparadigm.jsonrpc.JSONSerializer.toJSON(JSONSerializer.java:259)
at Communicator.doGet(Communicator.java:33)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at filters.ExampleFilter.doFilter(ExampleFilter.java:149)
Make use of Jackson JSON parser.
Refer - Jackson Home
The only thing you need to do -
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> jsonMap = mapper.readValue(inputStream, Map.class);
Now jsonMap will contain the JSON.
ObjectMapper.readTree(InputStream) easily let's you get nested JSON with JsonNodes.
public void testMakeCall() throws IOException {
URL url = new URL("https://api.coindesk.com/v1/bpi/historical/close.json?start=2010-07-17&end=2018-07-03");
HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
httpcon.addRequestProperty("User-Agent", "Mozilla/4.0");
InputStream is = httpcon.getInputStream();
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonMap = mapper.readTree(is);
JsonNode bpi = jsonMap.get("bpi");
JsonNode day1 = bpi.get("2010-07-18");
System.out.println(bpi.toString());
System.out.println(day1.toString());
} finally {
is.close();
}
}
Result:
{"2010-07-18":0.0858,"2010-07-19":0.0808,...}
0.0858
Better to save memory by having output as Stream<JsonNode>
private fun InputStream.toJsonNodeStream(): Stream<JsonNode> {
return StreamSupport.stream(
Spliterators.spliteratorUnknownSize(this.toJsonNodeIterator(), Spliterator.ORDERED),
false
)
}
private fun InputStream.toJsonNodeIterator(): Iterator<JsonNode> {
val jsonParser = objectMapper.factory.createParser(this)
return object: Iterator<JsonNode> {
override fun hasNext(): Boolean {
var token = jsonParser.nextToken()
while (token != null) {
if (token == JsonToken.START_OBJECT) {
return true
}
token = jsonParser.nextToken()
}
return false
}
override fun next(): JsonNode {
return jsonParser.readValueAsTree()
}
}
}
Related
I need to get a single value from JSON, but I get null as value.
public class PlaceTest {
public void sendRequest() throws Exception{
URIBuilder builder = new URIBuilder("https://maps.googleapis.com/maps/api/place/findplacefromtext/json");
builder.addParameter("key","mykey");
builder.addParameter("input","London");
builder.addParameter("inputtype","textquery");
HttpGet getReq = new HttpGet(builder.build());
HttpClient client = new DefaultHttpClient();
HttpResponse httpResponse = client.execute(getReq);
BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
StringBuilder builders = new StringBuilder();
String s;
while ((s = reader.readLine())!=null){
builders.append(s+"\n");
}
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(builders.toString());
JsonNode candidates = node.get("candidates");
System.out.println(candidates.get("place_id"));
//json
System.out.println(builders.toString());
}
}
The JSON code is:
{"candidates":[{"place_id" : "ChIJdd4hrwug2EcRmSrV3Vo6llI"}],"status" : "OK"}
In the above JSON candidates values is JsonArray
JsonNode node = mapper.readTree(builders.toString());
JsonNode candidates = node.get("candidates");
Now iterate the JsonArray and get place_id:
if (candidates.isArray()) {
for (JsonNode objNode : arrNode) {
System.out.println(objNode.get("place_id"));
}
}
I am able to return an HashMap as a JSON from my REST API built on Spring Boot. Here my Method:
#ResponseBody
#Transactional
#GetMapping("create_coinmarketcap_snapshot")
public ResponseEntity<HashMap> create_coinmarketcap_snapshot() {
String jsonString = callURL("https://api.coinmarketcap.com/v2/ticker/?limit=5");
JSONArray coinmarketcapsnapshotsArray = new JSONArray();
JSONObject coinmarketcapsnapshotsJSONObject = new JSONObject();
HashMap<Integer, CoinmarketcapSnapshot> coinmarketcapsnapshotsHashMap = new HashMap<>();
try {
JSONObject jsonObject = new JSONObject(jsonString);
JSONObject jsonObjectData = jsonObject.getJSONObject("data");
Iterator<?> keys = jsonObjectData.keys();
int count = 0;
while (keys.hasNext()) {
count++;
String key = (String) keys.next();
if (jsonObjectData.get(key) instanceof JSONObject) {
JSONObject jsonObjectDataCrypto = jsonObjectData.getJSONObject(key);
JSONObject jsonObjectDataCryptoQuotes = jsonObjectDataCrypto.getJSONObject("quotes").getJSONObject("USD");
CoinmarketcapSnapshot coinmarketcapsnapshotObject = new CoinmarketcapSnapshot();
String dateFormatted = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(Calendar.getInstance().getTime());
coinmarketcapsnapshotObject.setTitle(jsonObjectDataCrypto.get("name") + " - " + dateFormatted);
coinmarketcapsnapshotObject.setCryptocurrencyId((int) jsonObjectDataCrypto.get("id"));
if(jsonObjectDataCrypto.get("rank")!=null){
coinmarketcapsnapshotObject.setRank((int) jsonObjectDataCrypto.get("rank"));
}
if(jsonObjectDataCrypto.get("circulating_supply")!=null){
coinmarketcapsnapshotObject.setCirculatingSupply((Double) jsonObjectDataCrypto.get("circulating_supply"));
}
if(jsonObjectDataCrypto.get("total_supply")!=null){
coinmarketcapsnapshotObject.setTotalSupply((Double) jsonObjectDataCrypto.get("total_supply"));
}
if(!jsonObjectDataCrypto.isNull("circulating_supply")) {
coinmarketcapsnapshotObject.setMaxSupply((Double) jsonObjectDataCrypto.get("circulating_supply"));
}
if(!jsonObjectDataCrypto.isNull("total_supply")) {
coinmarketcapsnapshotObject.setMaxSupply((Double) jsonObjectDataCrypto.get("total_supply"));
}
if(!jsonObjectDataCrypto.isNull("max_supply")) {
coinmarketcapsnapshotObject.setMaxSupply((Double) jsonObjectDataCrypto.get("max_supply"));
}
if(!jsonObjectDataCryptoQuotes.isNull("price")) {
coinmarketcapsnapshotObject.setPrice((Double) jsonObjectDataCryptoQuotes.get("price"));
}
if(!jsonObjectDataCryptoQuotes.isNull("volume_24h")) {
coinmarketcapsnapshotObject.setVolume24h((Double) jsonObjectDataCryptoQuotes.get("volume_24h"));
}
if(!jsonObjectDataCryptoQuotes.isNull("market_cap")) {
coinmarketcapsnapshotObject.setMarketCap((Double) jsonObjectDataCryptoQuotes.get("market_cap"));
}
if(!jsonObjectDataCryptoQuotes.isNull("percent_change_1h")) {
coinmarketcapsnapshotObject.setPercentChange1h((Double) jsonObjectDataCryptoQuotes.get("percent_change_1h"));
}
if(!jsonObjectDataCryptoQuotes.isNull("percent_change_24h")) {
coinmarketcapsnapshotObject.setPercentChange24h((Double) jsonObjectDataCryptoQuotes.get("percent_change_24h"));
}
if(!jsonObjectDataCryptoQuotes.isNull("percent_change_7d")) {
coinmarketcapsnapshotObject.setPercentChange7d((Double) jsonObjectDataCryptoQuotes.get("percent_change_7d"));
}
entityManager.persist(coinmarketcapsnapshotObject);
coinmarketcapsnapshotsArray.put(coinmarketcapsnapshotObject);
coinmarketcapsnapshotsJSONObject.put(String.valueOf(count),coinmarketcapsnapshotObject);
coinmarketcapsnapshotsHashMap.put(count, coinmarketcapsnapshotObject);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println("\n\ncoinmarketcapsnapshotsArray:\n"+coinmarketcapsnapshotsArray);
System.out.println("\n\ncoinmarketcapsnapshotsJSONObject:\n"+coinmarketcapsnapshotsJSONObject);
System.out.println("\n\ncoinmarketcapsnapshotsHashMap:\n"+coinmarketcapsnapshotsHashMap);
return new ResponseEntity<>(coinmarketcapsnapshotsHashMap, HttpStatus.OK);
}
Here is what is printed in the terminal:
coinmarketcapsnapshotsArray:
["com.krown.entity.CoinmarketcapSnapshot#4d60f69f","com.krown.entity.CoinmarketcapSnapshot#4739c2f2","com.krown.entity.CoinmarketcapSnapshot#7d5bd573","com.krown.entity.CoinmarketcapSnapshot#43b5eb6d","com.krown.entity.CoinmarketcapSnapshot#26e1a633"]
coinmarketcapsnapshotsJSONObject:
{"1":"com.krown.entity.CoinmarketcapSnapshot#4d60f69f","2":"com.krown.entity.CoinmarketcapSnapshot#4739c2f2","3":"com.krown.entity.CoinmarketcapSnapshot#7d5bd573","4":"com.krown.entity.CoinmarketcapSnapshot#43b5eb6d","5":"com.krown.entity.CoinmarketcapSnapshot#26e1a633"}
coinmarketcapsnapshotsHashMap:
{1=com.krown.entity.CoinmarketcapSnapshot#4d60f69f, 2=com.krown.entity.CoinmarketcapSnapshot#4739c2f2, 3=com.krown.entity.CoinmarketcapSnapshot#7d5bd573, 4=com.krown.entity.CoinmarketcapSnapshot#43b5eb6d, 5=com.krown.entity.CoinmarketcapSnapshot#26e1a633}
I want to return my JSONObject "coinmarketcapsnapshotsJSONObject" instead "coinmarketcapsnapshotsHashMap", but when I do it, I keep getting stuck with this error:
No converter found for return value of type: class org.json.JSONObject
As suggested in some posts found on web, I added Jackson as new dependency in pom.xml file:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.0</version>
</dependency>
Unfortunately this didn't change anything.
Do you have any suggestion to improve the process of building a JSON for a REST API on Spring Boot?
When I return the HashMap, the output looks like that:
#GetMapping(produces={MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity<?> create_coinmarketcap_snapshot() throws IOException {
UriComponentsBuilder builder =
UriComponentsBuilder.fromUriString("https://api.coinmarketcap.com/v2/ticker")
.queryParam("limit", "5");
ResponseEntity<String> response =
restTemplate.getForEntity(builder.toUriString(), String.class);
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(response.getBody());
JsonNode data = root.path("data");
data.forEach(jsonObject -> {
jsonObject.get("rank"); //extracting values from each json object
jsonObject.get("circulating_supply");
jsonObject.get("total_supply");
jsonObject.get("max_supply");
jsonObject.get("price");
jsonObject.get("volume_24h");
jsonObject.get("market_cap");
jsonObject.get("percent_change_1h");
jsonObject.get("percent_change_24h");
//... and so on
});
return ResponseEntity.ok(data);
}
Now you're returning a json object that contains the value of "data" key #118218
HttpStatus.OK is the default return value for Http endpoints using Spring and therefore specifying it is unnecessary, thereby rendering the entire ResponseEntity unnecessary:
#ResponseBody
#Transactional
#GetMapping("create_coinmarketcap_snapshot")
public HashMap create_coinmarketcap_snapshot() {
String jsonString = callURL("https://api.coinmarketcap.com/v2/ticker/?limit=5");
JSONArray coinmarketcapsnapshotsArray = new JSONArray();
JSONObject coinmarketcapsnapshotsJSONObject = new JSONObject();
HashMap<Integer, CoinmarketcapSnapshot> coinmarketcapsnapshotsHashMap = new HashMap<>();
try {
JSONObject jsonObject = new JSONObject(jsonString);
JSONObject jsonObjectData = jsonObject.getJSONObject("data");
Iterator<?> keys = jsonObjectData.keys();
int count = 0;
while (keys.hasNext()) {
count++;
String key = (String) keys.next();
if (jsonObjectData.get(key) instanceof JSONObject) {
JSONObject jsonObjectDataCrypto = jsonObjectData.getJSONObject(key);
JSONObject jsonObjectDataCryptoQuotes = jsonObjectDataCrypto.getJSONObject("quotes").getJSONObject("USD");
CoinmarketcapSnapshot coinmarketcapsnapshotObject = new CoinmarketcapSnapshot();
String dateFormatted = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(Calendar.getInstance().getTime());
coinmarketcapsnapshotObject.setTitle(jsonObjectDataCrypto.get("name") + " - " + dateFormatted);
coinmarketcapsnapshotObject.setCryptocurrencyId((int) jsonObjectDataCrypto.get("id"));
if(jsonObjectDataCrypto.get("rank")!=null){
coinmarketcapsnapshotObject.setRank((int) jsonObjectDataCrypto.get("rank"));
}
if(jsonObjectDataCrypto.get("circulating_supply")!=null){
coinmarketcapsnapshotObject.setCirculatingSupply((Double) jsonObjectDataCrypto.get("circulating_supply"));
}
if(jsonObjectDataCrypto.get("total_supply")!=null){
coinmarketcapsnapshotObject.setTotalSupply((Double) jsonObjectDataCrypto.get("total_supply"));
}
if(!jsonObjectDataCrypto.isNull("circulating_supply")) {
coinmarketcapsnapshotObject.setMaxSupply((Double) jsonObjectDataCrypto.get("circulating_supply"));
}
if(!jsonObjectDataCrypto.isNull("total_supply")) {
coinmarketcapsnapshotObject.setMaxSupply((Double) jsonObjectDataCrypto.get("total_supply"));
}
if(!jsonObjectDataCrypto.isNull("max_supply")) {
coinmarketcapsnapshotObject.setMaxSupply((Double) jsonObjectDataCrypto.get("max_supply"));
}
if(!jsonObjectDataCryptoQuotes.isNull("price")) {
coinmarketcapsnapshotObject.setPrice((Double) jsonObjectDataCryptoQuotes.get("price"));
}
if(!jsonObjectDataCryptoQuotes.isNull("volume_24h")) {
coinmarketcapsnapshotObject.setVolume24h((Double) jsonObjectDataCryptoQuotes.get("volume_24h"));
}
if(!jsonObjectDataCryptoQuotes.isNull("market_cap")) {
coinmarketcapsnapshotObject.setMarketCap((Double) jsonObjectDataCryptoQuotes.get("market_cap"));
}
if(!jsonObjectDataCryptoQuotes.isNull("percent_change_1h")) {
coinmarketcapsnapshotObject.setPercentChange1h((Double) jsonObjectDataCryptoQuotes.get("percent_change_1h"));
}
if(!jsonObjectDataCryptoQuotes.isNull("percent_change_24h")) {
coinmarketcapsnapshotObject.setPercentChange24h((Double) jsonObjectDataCryptoQuotes.get("percent_change_24h"));
}
if(!jsonObjectDataCryptoQuotes.isNull("percent_change_7d")) {
coinmarketcapsnapshotObject.setPercentChange7d((Double) jsonObjectDataCryptoQuotes.get("percent_change_7d"));
}
entityManager.persist(coinmarketcapsnapshotObject);
coinmarketcapsnapshotsArray.put(coinmarketcapsnapshotObject);
coinmarketcapsnapshotsJSONObject.put(String.valueOf(count),coinmarketcapsnapshotObject);
coinmarketcapsnapshotsHashMap.put(count, coinmarketcapsnapshotObject);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println("\n\ncoinmarketcapsnapshotsArray:\n"+coinmarketcapsnapshotsArray);
System.out.println("\n\ncoinmarketcapsnapshotsJSONObject:\n"+coinmarketcapsnapshotsJSONObject);
System.out.println("\n\ncoinmarketcapsnapshotsHashMap:\n"+coinmarketcapsnapshotsHashMap);
return coinmarketcapsnapshotsHashMap;
}
I would like to convert a Java Properties file into JSON with nested objects. I followed an example I found here but I can't get it to work.
For example, given:
objectA.version=1.0
objectA.hostname=192.168.0.11
objectA.port=9989
objectB.hostname=10.0.2.15
objectB.port=9998
I want:
{
“objectA” : {
"version” : "1.0"
"host” : “192.168.0.11”,
"port" : 9989
},
“objectB” : {
"host” : “10.0.2.15”,
"port" : 9998
}
}
This is what I have so far:
static class Endpoint
{
#JsonProperty("objectA")
public ObjectA objectA;
#JsonProperty("objectB")
public ObjectB objectB;
}
static class ObjectA
{
public String hostname;
public String port;
public String version;
}
static class ObjectB
{
public String hostname;
public String port;
}
try (InputStream input = getClass().getClassLoader().getResourceAsStream("file.properties"))
{
JavaPropsMapper mapper = new JavaPropsMapper();
Endpoint host = mapper.readValue(input, Endpoint.class);
String asText = mapper.writeValueAsString(host);
System.out.println(asText);
}
The output looks like this:
objectA.version=1.0
objectA.hostname=192.168.0.11
objectA.port=9989
objectB.hostname=10.0.2.15
objectB.port=9998
I figured it out.
try (InputStream input = getClass().getClassLoader().getResourceAsStream("file.properties"))
{
JavaPropsMapper mapper = new JavaPropsMapper();
Endpoint host = mapper.readValue(input, Endpoint.class);
// String asText = mapper.writeValueAsString(host);
// add this
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String asText = ow.writeValueAsString(host);
//
System.out.println(asText);
}
Not sure if the OP needs an interim object OR if he just wants to get from a props file to a JSON representation of it. If it is the latter then it's easier to just use the databind ObjectNode class as the interim. E.g.
try (InputStream input = new FileInputStream("path_to.properties")) {
JavaPropsMapper mapper = new JavaPropsMapper();
ObjectNode node = mapper.readValue(input, ObjectNode.class);
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
// Alternatively write to file ???
System.out.println(ow.writeValueAsString(node);
} catch (IOException e) {
// Do something
}
I want to communicate with a web server and exchange JSON information.
my webservice URL looking like following format: http://46.157.263.140/EngineTestingWCF/DPMobileBookingService.svc/SearchOnlyCus
Here is my JSON Request format.
{
"f": {
"Adults": 1,
"CabinClass": 0,
"ChildAge": [
7
],
"Children": 1,
"CustomerId": 0,
"CustomerType": 0,
"CustomerUserId": 81,
"DepartureDate": "/Date(1358965800000+0530)/",
"DepartureDateGap": 0,
"Infants": 1,
"IsPackageUpsell": false,
"JourneyType": 2,
"PreferredCurrency": "INR",
"ReturnDate": "/Date(1359138600000+0530)/",
"ReturnDateGap": 0,
"SearchOption": 1
},
"fsc": "0"
}
I tried with the following code to send a request:
public class Fdetails {
private String Adults = "1";
private String CabinClass = "0";
private String[] ChildAge = { "7" };
private String Children = "1";
private String CustomerId = "0";
private String CustomerType = "0";
private String CustomerUserId = "0";
private Date DepartureDate = new Date();
private String DepartureDateGap = "0";
private String Infants = "1";
private String IsPackageUpsell = "false";
private String JourneyType = "1";
private String PreferredCurrency = "MYR";
private String ReturnDate = "";
private String ReturnDateGap = "0";
private String SearchOption = "1";
}
public class Fpack {
private Fdetails f = new Fdetails();
private String fsc = "0";
}
Then using Gson I create the JSON object like:
public static String getJSONString(String url) {
String jsonResponse = null;
String jsonReq = null;
Fpack fReq = new Fpack();
try {
Gson gson = new Gson();
jsonReq = gson.toJson(fReq);
JSONObject json = new JSONObject(jsonReq);
JSONObject jsonObjRecv = HttpClient.SendHttpPost(url, json);
jsonResponse = jsonObjRecv.toString();
}
catch (JSONException e) {
e.printStackTrace();
}
return jsonResponse;
}
and my HttpClient.SendHttpPost method is
public static JSONObject SendHttpPost(String URL, JSONObject json) {
try {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPostRequest = new HttpPost(URL);
StringEntity se;
se = new StringEntity(json.toString());
httpPostRequest.setEntity(se);
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
HttpEntity entity = response.getEntity();
if (entity != null) {
// Read the content stream
InputStream instream = entity.getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
instream = new GZIPInputStream(instream);
}
// convert content stream to a String
String resultString= convertStreamToString(instream);
instream.close();
resultString = resultString.substring(1,resultString.length()-1); // remove wrapping "[" and "]"
// Transform the String into a JSONObject
JSONObject jsonObjRecv = new JSONObject(resultString);
return jsonObjRecv;
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
Now I get the following exception:
org.json.JSONException: Value !DOCTYPE of type java.lang.String cannot be converted to JSONObject
at org.json.JSON.typeMismatch(JSON.java:111)
at org.json.JSONObject.<init>(JSONObject.java:158)
at org.json.JSONObject.<init>(JSONObject.java:171)
and the printout of JSON string right before I make the request is as follows:
{
"f": {
"PreferredCurrency": "MYR",
"ReturnDate": "",
"ChildAge": [
7
],
"DepartureDate": "Mar 2, 2013 1:17:06 PM",
"CustomerUserId": 0,
"CustomerType": 0,
"CustomerId": 0,
"Children": 1,
"DepartureDateGap": 0,
"Infants": 1,
"IsPackageUpsell": false,
"JourneyType": 1,
"CabinClass": 0,
"Adults": 1,
"ReturnDateGap": 0,
"SearchOption": 1
},
"fsc": "0"
}
How do I solve this exception? Thanks in advance!
To create a request with JSON object attached to it what you should do is the following:
public static String sendComment (String commentString, int taskId, String sessionId, int displayType, String url) throws Exception
{
Map<String, Object> jsonValues = new HashMap<String, Object>();
jsonValues.put("sessionID", sessionId);
jsonValues.put("NewTaskComment", commentString);
jsonValues.put("TaskID" , taskId);
jsonValues.put("DisplayType" , displayType);
JSONObject json = new JSONObject(jsonValues);
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url + SEND_COMMENT_ACTION);
AbstractHttpEntity entity = new ByteArrayEntity(json.toString().getBytes("UTF8"));
entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(entity);
HttpResponse response = client.execute(post);
return getContent(response);
}
I'm not quite familiar with Json, but I know it's pretty commonly used today, and your code seems no problem.
How to convert this JSON string to JSON object?
Well, you almost get there, just send the JSON string to your server, and use Gson again in your server:
Gson gson = new Gson();
Fpack f = gson.fromJSON(json, Fpack.class);
http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/index.html
About the Exception:
You should remove this line, because you are sending a request, not responsing to one:
httpPostRequest.setHeader("Accept", "application/json");
And I would change this line:
httpPostRequest.setHeader("Content-type", "application/json");
to
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
If this doesn't make any difference, please print out your JSON string before you send the request, let's see what's in there.
From what I have understood you want to make a request to the server using the JSON you have created, you can do something like this:
URL url;
HttpURLConnection connection = null;
String urlParameters ="json="+ jsonSend;
try {
url = new URL(targetURL);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Language", "en-US");
DataOutputStream wr = new DataOutputStream (
connection.getOutputStream ());
wr.writeBytes (urlParameters);
wr.flush ();
wr.close ();
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if(connection != null) {
connection.disconnect();
}
}
}
Actually it was a BAD REQUEST. Thats why server returns response as XML format.
The problem is to convert the non primitive data(DATE) to JSON object.. so it would be Bad Request..
I solved myself to understand the GSON adapters.. Here is the code I used:
try {
JsonSerializer<Date> ser = new JsonSerializer<Date>() {
#Override
public JsonElement serialize(Date src, Type typeOfSrc,
JsonSerializationContext comtext) {
return src == null ? null : new JsonPrimitive("/Date("+src.getTime()+"+05300)/");
}
};
JsonDeserializer<Date> deser = new JsonDeserializer<Date>() {
#Override
public Date deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext jsonContext) throws JsonParseException {
String tmpDate = json.getAsString();
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher(tmpDate);
boolean found = false;
while (matcher.find() && !found) {
found = true;
tmpDate = matcher.group();
}
return json == null ? null : new Date(Long.parseLong(tmpDate));
}
};
I am using java to call a url that returns a JSON object:
url = new URL("my URl");
urlInputStream = url.openConnection().getInputStream();
How can I convert the response into string form and parse it?
I would suggest you have to use a Reader to convert your InputStream in.
BufferedReader streamReader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
StringBuilder responseStrBuilder = new StringBuilder();
String inputStr;
while ((inputStr = streamReader.readLine()) != null)
responseStrBuilder.append(inputStr);
new JSONObject(responseStrBuilder.toString());
I tried in.toString() but it returns:
getClass().getName() + '#' + Integer.toHexString(hashCode())
(like documentation says it derives to toString from Object)
All the current answers assume that it is okay to pull the entire JSON into memory where the advantage of an InputStream is that you can read the input little by little. If you would like to avoid reading the entire Json file at once then I would suggest using the Jackson library (which is my personal favorite but I'm sure others like Gson have similar functions).
With Jackson you can use a JsonParser to read one section at a time. Below is an example of code I wrote that wraps the reading of an Array of JsonObjects in an Iterator. If you just want to see an example of Jackson, look at the initJsonParser, initFirstElement, and initNextObject methods.
public class JsonObjectIterator implements Iterator<Map<String, Object>>, Closeable {
private static final Logger LOG = LoggerFactory.getLogger(JsonObjectIterator.class);
private final InputStream inputStream;
private JsonParser jsonParser;
private boolean isInitialized;
private Map<String, Object> nextObject;
public JsonObjectIterator(final InputStream inputStream) {
this.inputStream = inputStream;
this.isInitialized = false;
this.nextObject = null;
}
private void init() {
this.initJsonParser();
this.initFirstElement();
this.isInitialized = true;
}
private void initJsonParser() {
final ObjectMapper objectMapper = new ObjectMapper();
final JsonFactory jsonFactory = objectMapper.getFactory();
try {
this.jsonParser = jsonFactory.createParser(inputStream);
} catch (final IOException e) {
LOG.error("There was a problem setting up the JsonParser: " + e.getMessage(), e);
throw new RuntimeException("There was a problem setting up the JsonParser: " + e.getMessage(), e);
}
}
private void initFirstElement() {
try {
// Check that the first element is the start of an array
final JsonToken arrayStartToken = this.jsonParser.nextToken();
if (arrayStartToken != JsonToken.START_ARRAY) {
throw new IllegalStateException("The first element of the Json structure was expected to be a start array token, but it was: " + arrayStartToken);
}
// Initialize the first object
this.initNextObject();
} catch (final Exception e) {
LOG.error("There was a problem initializing the first element of the Json Structure: " + e.getMessage(), e);
throw new RuntimeException("There was a problem initializing the first element of the Json Structure: " + e.getMessage(), e);
}
}
private void initNextObject() {
try {
final JsonToken nextToken = this.jsonParser.nextToken();
// Check for the end of the array which will mean we're done
if (nextToken == JsonToken.END_ARRAY) {
this.nextObject = null;
return;
}
// Make sure the next token is the start of an object
if (nextToken != JsonToken.START_OBJECT) {
throw new IllegalStateException("The next token of Json structure was expected to be a start object token, but it was: " + nextToken);
}
// Get the next product and make sure it's not null
this.nextObject = this.jsonParser.readValueAs(new TypeReference<Map<String, Object>>() { });
if (this.nextObject == null) {
throw new IllegalStateException("The next parsed object of the Json structure was null");
}
} catch (final Exception e) {
LOG.error("There was a problem initializing the next Object: " + e.getMessage(), e);
throw new RuntimeException("There was a problem initializing the next Object: " + e.getMessage(), e);
}
}
#Override
public boolean hasNext() {
if (!this.isInitialized) {
this.init();
}
return this.nextObject != null;
}
#Override
public Map<String, Object> next() {
// This method will return the current object and initialize the next object so hasNext will always have knowledge of the current state
// Makes sure we're initialized first
if (!this.isInitialized) {
this.init();
}
// Store the current next object for return
final Map<String, Object> currentNextObject = this.nextObject;
// Initialize the next object
this.initNextObject();
return currentNextObject;
}
#Override
public void close() throws IOException {
IOUtils.closeQuietly(this.jsonParser);
IOUtils.closeQuietly(this.inputStream);
}
}
If you don't care about memory usage, then it would certainly be easier to read the entire file and parse it as one big Json as mentioned in other answers.
For those that pointed out the fact that you can't use the toString method of InputStream like this see https://stackoverflow.com/a/5445161/1304830 :
My correct answer would be then :
import org.json.JSONObject;
public static String convertStreamToString(java.io.InputStream is) {
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
...
JSONObject json = new JSONObject(convertStreamToString(url.openStream());
If you like to use Jackson Databind (which Spring uses by default for its HttpMessageConverters), then you may use the ObjectMapper.readTree(InputStream) API. For example,
ObjectMapper mapper = new ObjectMapper();
JsonNode json = mapper.readTree(myInputStream);
use jackson to convert json input stream to the map or object http://jackson.codehaus.org/
there are also some other usefull libraries for json, you can google: json java
Use a library.
GSON
Jackson
or one of many other JSON libraries that are out there.
Kotlin version with Gson
to read the response JSON:
val response = BufferedReader(
InputStreamReader(conn.inputStream, "UTF-8")
).use { it.readText() }
to parse response we can use Gson:
val model = Gson().fromJson(response, YourModelClass::class.java)
This example reads all objects from a stream of objects,
it is assumed that you need CustomObjects instead of a Map:
ObjectMapper mapper = new ObjectMapper();
JsonParser parser = mapper.getFactory().createParser( source );
if(parser.nextToken() != JsonToken.START_ARRAY) {
throw new IllegalStateException("Expected an array");
}
while(parser.nextToken() == JsonToken.START_OBJECT) {
// read everything from this START_OBJECT to the matching END_OBJECT
// and return it as a tree model ObjectNode
ObjectNode node = mapper.readTree(parser);
CustomObject custom = mapper.convertValue( node, CustomObject.class );
// do whatever you need to do with this object
System.out.println( "" + custom );
}
parser.close();
This answer was composed by using : Use Jackson To Stream Parse an Array of Json Objects and Convert JsonNode into Object
I suggest use javax.json.Json factory as less verbose possible solution:
JsonObject json = Json.createReader(yourInputStream).readObject();
Enjoy!
if you have JSON file you can set it on assets folder then call it using this code
InputStream in = mResources.getAssets().open("fragrances.json");
// where mResources object from Resources class
{
InputStream is = HTTPClient.get(url);
InputStreamReader reader = new InputStreamReader(is);
JSONTokener tokenizer = new JSONTokener(reader);
JSONObject jsonObject = new JSONObject(tokenizer);
}