Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert InputStream into JSON

Tags:

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) 
like image 813
Margi Avatar asked Sep 13 '13 20:09

Margi


People also ask

How to create JSON object from InputStream In java?

InputStream inputStreamObject = PositionKeeperRequestTest. class. getResourceAsStream(jsonFileName); BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputStreamObject, "UTF-8")); StringBuilder responseStrBuilder = new StringBuilder(); String inputStr; while ((inputStr = streamReader.

Can we convert String to JSON in Java?

There are the following three libraries are used to convert String to JSON Object in Java: Using Gson Library. Using JSON-Simple Library. Jackson Library.

What is JSONTokener in Java?

A JSONTokener takes a source string and extracts characters and tokens from it. It is used by the JSONObject and JSONArray constructors to parse JSON source strings.

What is a JSONObject Java?

A JSONObject is an unordered collection of key and value pairs, resembling Java's native Map implementations. Keys are unique Strings that cannot be null. Values can be anything from a Boolean, Number, String, or JSONArray to even a JSONObject. NULL object.


2 Answers

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.

like image 182
Vishal Pawale Avatar answered Sep 28 '22 06:09

Vishal Pawale


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

like image 35
Philip Rego Avatar answered Sep 28 '22 06:09

Philip Rego