I'm trying to save the tweets I got as JSON objects and however still unable to get the JSON object out of Status objects.
I have checked JSONStoreEnabled() and it's set to true.
API says,
Note that raw JSON forms can be retrieved only from the same thread
invoked the last method call and will become inaccessible once another
method call.
I have my everything inside the main() method, so I guess this shouldn't be an issue.
String tweet = TwitterObjectFactory.getRawJSON( status );
I have checked that the status object contains all the information and it's just that getRawJSON returns null!.
Really appreciate if someone can tell me a fix for this.
I am using this approach in my own app. You can try this while configuring Twitter4j.
Using com.google.gson.Gson;
public Gson gson = new Gson();
ConfigurationBuilder config = new ConfigurationBuilder();
config.setJSONStoreEnabled(true);
config.setOAuthConsumerKey(Keys.TWITTER_KEY);
config.setOAuthConsumerSecret(Keys.TWITTER_SECRET);
config.setOAuthAccessToken(currentSession.getAuthToken().token);
config.setOAuthAccessTokenSecret(currentSession.getAuthToken().secret);
Configuration cf = config.build();
// For Twitter4j
enter code here
String statusJson = TwitterObjectFactory.getRawJSON(status); // status to json
This is it.
Gson gson = new Gson();
String json = gson.toJson( status );
System.out.println( json );
new twitter4j.JSONObject(status)
it is works for me
Related
ConfigurationBuilder cb = new ConfigurationBuilder();
//... set your keys
String queryString = "Trump";
Twitter twitter = new TwitterFactory(cb.build()).getInstance();
result = twitter.search(query);
List<Status> tweets = result.getTweets();
String json = TwitterObjectFactory.getRawJSON(s);
and I get :
java.lang.IllegalStateException: Apparently jsonStoreEnabled is not
set to true.
Question 1:
I looked through the source code and looks like jsonStoreEnabled is set on ThreadLocal by a call to TwitterObjectFactory.registerJSONObject . I don't think search does that . Does it mean its impossible to get raw json when calling search API ?
Question2:
How do I intercept a call made inside the API to TwitterBaseImpl.httpResponseReceived to get the value of the field before its obscured by layers of the framework ? preferably without AspectJ
Seelenvirtuose is correct , I forgot to do cb.setJSONStoreEnabled(true)
I am working on a back-end project where request is coming as JSON object to my servlet and response is sent as an JSON object as well. I found this LINK useful but need help to understand how filter can be used in my aforementioned back-end scenario to globalized my all classes/APIs through filter
I am new to Java and looking for advice that is it possible? OR I have to manually call ResourceBundle for each class
Thanks in anticipation
If you are receiving your i18n in json payload, you may try doing something as following
// this parses the json
JSONObject jObj = new JSONObject(request.getParameter("yourPramName"));
Iterator it = jObj.keys();
while(it.hasNext())
{
String key = it.next(); // get key
Object o = jObj.get(key); // get value
req.getSession().setAttribute(key, o);
}
Hope this helps.
I tried uploading an excel file and it worked well but the return message doesn't show. For the return message I use the below code as shown here:
JSONObject myObj = new JSONObject();
//sets success to true
myObj.put("success", true);
//convert the JSON object to string and send the response back
out.println(myObj.toString());
out.close();
For using this class I used the json-lib.jar. Further it asks for dependent jars like ezmorph-1.0.jar which I am doubtful of using.
Has anyone used the above method to return a message to the front end?
If so, what were the jars used in the process? Please help
I resolved the above by using json-simple-1.1.jar instead of json-lib.jar.
This created a JSONObject without the need of any other jar.
I've build a web application that produces XML code of an object. To my suprise, the xml produced is completely correct and in the format I wanted it. However, I'm now making a method that consumes XML in the same format and turn it back in an object. How can I test if it is working?
I've tried using a REST extension in chrome that posts the exact same XML that my other method produces, but I get the error: "The server refused this request because the request entity is in a format not supported by the requested resource for the requested method." I've also tried putting breakpoints in my code and debugging it that way, but my breakpoints are never even reached.
#GET
#Produces(MediaType.TEXT_XML)
public week_program getXml() {
week_program weekProgram = new week_program();
return weekProgram;
}
#POST
#Consumes(MediaType.TEXT_XML)
public Response PostXml(week_program weekProgram) {
System.out.println(weekProgram);
return Response.status(Status.OK).entity(weekProgram).build();
}
How can I fix it, or even test correctly if it does actually work?
I would suggest using json instead of XML and Gson from Google.
Since json output is usually smaller than XML (fat free).
Object to JSON
DataObject obj = new DataObject();
Gson gson = new Gson();
String json_string = gson.toJson(obj);
JSON to Object
DataObject obj = gson.fromJson(json_string, DataObject.class);
Here's a tutorial. http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/
Hello i'm trying to convert my Java Object to a Json string to use in my view with javascript functions.
I tried to use Google's gson lib:
String myjson = "";
Gson gson = new Gson();
myjson = gson.toJson(myuser);//myuser is my java object
However when i call myjson, it consists of '"' instead of the character '"' for this reason i can't able to use it in my view, it is creating me problems.
How can I fix this, how can I create Json string properly?
Edit : Is there a chance of making a mistake while rendering the myjson?
Edit2: To reach the result of myjson in view, i'm doing
render(myjson);
at the end of my code.
Edit3: Play framework has a method of renderJSON(). However i can't use it since i'm rendering not only myjson and some other elements too.
Note that """ is "Proper" JSON, so your javascript can be escaped properly, anyways if you dont want to use entities in you JSON perhaps you want to disable html formatting/escaping.
Try using
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
Check the GSONBuilder Object Documentation http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/GsonBuilder.html
I think he's protecting you against html special chars.
Try something like that:
Gson gson = new Gson();
String target = "my text";
String json = gson.toJson(target);
This code is extract from http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/Gson.html (the api).