can't split json string as parameter in jsonarray - java

I have This code and I tried to getting items from this string
but it's failed
I have used eclipse IDE
I have parsing the json string from remote host
package selectDB;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class selectDB
{
public static void main(String[] args) throws IOException, ParseException
{
String line;
String s = "";
URL u = new URL("http://192.168.3.1/android/select.php");
URLConnection c = u.openConnection();
InputStream r = c.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(r));
for(; (line = reader.readLine()) != null;)
{
s+=line;
}
System.out.println(s);
try{
JSONObject jObject = new JSONObject(s);
String projecname=(String) jObject.get("name");
System.out.print(projecname);
}catch(Exception e)
{}
}
}
the result Json string is like this
{"result" : "true" , "messages" : [{"id":"866343023633578","latitute":"27","longitude":"31","number_phone":"01113171374"},{"id":"352168066354050","latitute":"27","longitude":"31","number_phone":"202222"},{"id":"50","latitute":"50","longitude":"100","number_phone":"50"},{"id":"110","latitute":"50","longitude":"50","number_phone":"110"},{"id":"120","latitute":"27","longitude":"31","number_phone":"120"},{"id":"130","latitute":"28","longitude":"29","number_phone":"120"},{"id":"140","latitute":"30","longitude":"40","number_phone":"140"},{"id":"800","latitute":"60","longitude":"30","number_phone":"800"},{"id":"353629054230064","latitute":"70","longitude":"80","number_phone":"120"}]}
please help me
thanks

You have to parse the json string into JSONObject using JSONParser
You cannot simply parameterise JSONObject with a string.
So you have to do:
JSONParser parser = new JSONParser();
JSONObject jObject=(JSONObject)parser.parse(jsonstr);
String projecname=(String) jObject.get("name");

Related

When converting a Json to an Avro file only 3/4 of the lines are converted, what am I doing wrong?

This my code:
package hadoopPlayground;
import java.io.BufferedReader;
import org.apache.avro.Schema;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.avro.file.DataFileWriter;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.generic.GenericDatumWriter;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.io.DatumReader;
import org.apache.avro.io.Decoder;
import org.apache.avro.io.DecoderFactory;
import org.apache.avro.io.Encoder;
import org.apache.avro.io.EncoderFactory;
import org.apache.commons.io.IOUtils;
public class json2AVRO {
public static void main( String[] args ) throws Exception
{
String filename = "ds214_arrivi_mensili.json";
File JSONFile = new File(filename);
String filename2 = "ds214_arrivi_mensili.avsc";
File AVSCFile = new File(filename2);
BufferedReader read = new BufferedReader(new FileReader(JSONFile));
BufferedReader read2 = new BufferedReader(new FileReader(AVSCFile));
String outputName = JSONFile.toString().substring(0,
JSONFile.toString().lastIndexOf(".")) + ".avro";
String json = org.apache.commons.io.IOUtils.toString(read);
String schema = org.apache.commons.io.IOUtils.toString(read2);
InputStream input;
Encoder encoder;
ByteArrayOutputStream output;
Schema schema1 = new Schema.Parser().parse(AVSCFile);
DatumReader<GenericRecord> reader = new GenericDatumReader<GenericRecord>(schema1);
input = new ByteArrayInputStream(json.getBytes());
output = new ByteArrayOutputStream();
DataInputStream din = new DataInputStream(input);
Decoder decoder = DecoderFactory.get().jsonDecoder(schema1, din);
System.out.println(decoder);
encoder = EncoderFactory.get().binaryEncoder(output, null);
GenericRecord datum;
GenericDatumWriter<GenericRecord> writer1 = new GenericDatumWriter<GenericRecord>(schema1);
File file= new File(outputName);
DataFileWriter<GenericRecord> dataWriter = new DataFileWriter<GenericRecord>(writer1);
dataWriter.create(schema1, file);
try
{
for (int i = 0; i < json.length(); i++) {
datum = reader.read(null, decoder);
dataWriter.append(datum);
System.out.println(datum);
output.close();
}
}
catch (IOException e)
{
}
finally
{
//Here is the flushing and closing
try
{
if (encoder != null)
{
encoder.flush();
}
if (output != null)
{
output.close();
}
} catch (IOException e)
{
throw new RuntimeException(e);
}
}
}
}
The file is converted to AVRO correctly (apparently), but when I try to convert my AVRO to JSON from the terminal it only shows 1692 lines out of the 2016 expected... what's wrong?
I've already checked my JSON and converting my JSON to AVRO from the terminal I have no problem whatsoever. There are no strange symbols at line 1692 btw.

Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at [Source: java.io.StringReader#505fd738; line: 1, column: 1]

I am getting error while doing deserialization.
I have an URL and I am getting JSON data from that URL, by using the code below in sb variable but how can I deserialize it by using this code?
My JSON data is:
{ "status": "success",
"msg": [
{ "fav_food": "roti",
"user_name": "123",
"email": "123#gmail.com" },
{ "fav_food": "iii",
"user_name": "343",
"email": "234#gmail.co" },
{ "fav_food": "paneer",
"user_name": "343",
"email": "343#gmail.com"}
]
}
Code:
package com.pack;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
#SuppressWarnings("serial")
#JsonIgnoreProperties(ignoreUnknown = true)
public class Collection_to_jacksonJsonServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws
IOException {
resp.setContentType("text/html");
PrintWriter out=resp.getWriter();
Map<String,Directory> contacts=new HashMap<String,Directory>();
ObjectMapper ob=new ObjectMapper();
List<Data> l=new ArrayList<Data>();
URL url=new URL("https://api-demo-py.appspot.com/getAllUsers");
HttpURLConnection request = (HttpURLConnection) url.openConnection();
request.connect();
StringBuilder sb=new StringBuilder();
BufferedReader br=new BufferedReader(new
InputStreamReader(request.getInputStream()));
String line;
try
{
//read from the urlConnection via the bufferedReader
while ((line = br.readLine()) != null)
{
sb.append(line);
}
br.close();
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println(sb);
String jsonString=sb.toString();
l= ob.readValue(jsonString,new TypeReference<List<Data>>(){});
}
}
well you have to deserialize to object of following:
class Wrapper {
public String status;
public List<Data> msg;
}

The constructor JSONObject(String) is undefined error - not able to resolve

I am trying to get some variables in servlet which are all passed through ajax call. But i am getting constructor JSONobject(string) is undefined error. I have imported required lib also. Please help
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.json.*;
public class insertserv extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
System.out.println("I am inside insert");
String json = "";
BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
if (br != null) {
json = br.readLine();
}
System.out.println(json);
JSONObject wholedata= new JSONObject(json);
response.setContentType("application/json");
PrintWriter out= response.getWriter();
}
}
removed org.json.simple.JSONObject; and updated with org.json.JSONObject; its solved my issue

How to get key taken from data taken from website API

I am currently coding in Java using Eclipse. I am trying to obtain the value of the key "high" given the data
{"high": "639.00000", "last": "634.94000", "timestamp": "1476220216", "bid": "634.94000", "vwap": "630.07099", "volume": "7939.75947138", "low": "613.83000", "ask": "636.50000", "open": "616.37000"}
which I get from this address : "https://www.bitstamp.net/api/v2/ticker/btcusd/"
So far, I got this code:
package JsonSimpleExample;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import org.json.simple.*;
public class JsonSimpleExample {
public static void main(String[] args) throws IOException {
URL url = new URL("https://www.bitstamp.net/api/v2/ticker/btcusd/");
URLConnection con = url.openConnection();
InputStream is =con.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
System.out.println(br.readLine());
}
}
A possible solution is to write the data I get from the website into a file and input each key, value pair into a hashmap and call for the value but that seems very highly redundant. Is there any way to directly obtain a value for a key with the data that the API gives me?
Simple you need to add json library also.
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
import org.json.JSONObject;
/**
*
* #author BTACTC
*/
public class JsonExample {
URL url;
String str;
Scanner scan;
JSON json;
public JsonExample() throws MalformedURLException, IOException {
url = new URL("https://www.bitstamp.net/api/v2/ticker/btcusd/");
scan = new Scanner(url.openStream());
str = new String();
while (scan.hasNext()) {
str += scan.nextLine();
}
System.out.println(str);// checking the data is now in string
JSONObject obj = new JSONObject(str);
System.out.println(obj.getString("high"));;
}
public static void main(String[] args) throws IOException {
JsonExample JE = new JsonExample();
}
}

JSONException Twitter client side application

I am currently trying to build a client application for twitter. One of the functionalities of the app is to search tweet (including historical tweet). I tried to modify the code that I got from Github. However, when I tried to debug the code, I got JSONException cause by null value. Here is my code:
package thematicanalysis;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;
import twitter4j.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import twitter4j.JSONException;
/**
*
* #author adichris
*/
public class TweetManager {
private static String getURLResponse(String since, String until, String querySearch, String scrollCursor,int counter) throws Exception{
String appendQuery = " ";
if(since!=null)
appendQuery+= " since:"+since;
if(until!=null)
appendQuery+= " until:"+until;
if(querySearch!=null)
appendQuery+= " "+querySearch;
String url = String.format("https://twitter.com/search?src=typd&q=%s&scroll_cursor=%s", URLEncoder.encode(appendQuery, "UTF-8"),scrollCursor);
System.out.println("URL: "+ url);
URL obj = new URL (url);
HttpURLConnection con = (HttpURLConnection)obj.openConnection();
con.setRequestMethod("GET");
//StringBuilder response;
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while((inputLine=in.readLine())!=null)
response.append(inputLine);
in.close();
con.disconnect();
//System.out.println(response.toString());
saveToFile(response.toString(),counter);
return response.toString();
}
private static void saveToFile(String content,int counter) throws IOException
{
try (PrintWriter pw = new PrintWriter("newOutput"+counter+".txt","UTF-8")) {
pw.printf("%s\n",content);
pw.close();
}
}
public static void getTweets (String since, String until, String querySearch) throws JSONException, Exception{
try{
String refreshCursor = null;
int counter = 1;
while(true)
{
String response = getURLResponse(since,until,querySearch,refreshCursor,counter);
JSONObject json = new JSONObject(response);
if(json.equals(null))
System.out.println("hereeee");
counter++;
System.out.println(counter);
refreshCursor = json.getString("scroll_cursor");
Document doc = Jsoup.parse((String)json.get("items_html"));
Elements tweets = doc.select("div.js-stream-tweet");
System.out.println(tweets.size());
if (tweets.isEmpty()){
break;
}
for (Element tweet: tweets){
String userName = tweet.select("span.username.js-action-profile-name b").text();
String text = tweet.select("p.js-tweet-text").text().replaceAll("[^\\u0000-\\uFFFF]", "");
long dateMs = Long.valueOf(tweet.select("small.time span.js-short-timestamp").attr("data-time-ms"));
Date date = new Date(dateMs);
System.out.println(userName);
//saveToFile(text);
}
}
}catch(JSONException e){
e.printStackTrace();
}
}
}

Categories