Error while creating a sub JSONObject from another JSONObject - java

I am working on an Android app and one of its functionnalities is to know when a streamer is streaming by using Twitch API.
When a streamer is streaming, if I connect to https://api.twitch.tv/kraken/streams/ I get a String which I use to build a JSON object like that :
{
"_links":{
"self":"https://api.twitch.tv/kraken/streams/srkevo1",
"channel":"https://api.twitch.tv/kraken/channels/srkevo1"
},
"stream":{
"_id":15361851552,
"game":"Super Smash Bros. for Wii U",
"viewers":42613,
"created_at":"2015-07-18T15:07:59Z",
"video_height":720,
"average_fps":59.319897084,
"_links":{
"self":"https://api.twitch.tv/kraken/streams/srkevo1"
},
"preview":{
"small":"http://static-cdn.jtvnw.net/previews-ttv/live_user_srkevo1-80x45.jpg",
"medium":"http://static-cdn.jtvnw.net/previews-ttv/live_user_srkevo1-320x180.jpg",
"large":"http://static-cdn.jtvnw.net/previews-ttv/live_user_srkevo1-640x360.jpg",
"template":"http://static-cdn.jtvnw.net/previews-ttv/live_user_srkevo1-{width}x{height}.jpg"
},
"channel":{
"_links":{
"self":"https://api.twitch.tv/kraken/channels/srkevo1",
"follows":"https://api.twitch.tv/kraken/channels/srkevo1/follows",
"commercial":"https://api.twitch.tv/kraken/channels/srkevo1/commercial",
"stream_key":"https://api.twitch.tv/kraken/channels/srkevo1/stream_key",
"chat":"https://api.twitch.tv/kraken/chat/srkevo1",
"features":"https://api.twitch.tv/kraken/channels/srkevo1/features",
"subscriptions":"https://api.twitch.tv/kraken/channels/srkevo1/subscriptions",
"editors":"https://api.twitch.tv/kraken/channels/srkevo1/editors",
"videos":"https://api.twitch.tv/kraken/channels/srkevo1/videos",
"teams":"https://api.twitch.tv/kraken/channels/srkevo1/teams"
},
"background":null,
"banner":null,
"broadcaster_language":"en",
"display_name":"srkevo1",
"game":"Super Smash Bros. for Wii U",
"logo":"http://static-cdn.jtvnw.net/jtv_user_pictures/srkevo1-profile_image-e46c53476d9b74c7-300x300.png",
"mature":null,
"status":"Evolution 2015 - Main Stage (July 17-19) all brackets http://evo2015.s3.amazonaws.com/brackets/index.html",
"partner":true,
"url":"http://www.twitch.tv/srkevo1",
"video_banner":"http://static-cdn.jtvnw.net/jtv_user_pictures/srkevo1-channel_offline_image-ee2fc39d6ebb7735-640x360.jpeg",
"_id":30917811,
"name":"srkevo1",
"created_at":"2012-05-30T16:57:11Z",
"updated_at":"2015-07-18T16:18:40Z",
"delay":0,
"followers":82134,
"profile_banner":null,
"profile_banner_background_color":null,
"views":20938144,
"language":"en"
}
}
}
This is how I get the JSON Object :
public class GetStreamStatus extends AsyncTask<Void,Void,String> {
#Override
protected String doInBackground(Void... params) {
String res = bibixChannel.getJson("streams");
return res;
}
#Override
protected void onPostExecute(String s) {
channelStatusString = s;
channelStatusObject = bibixChannel.buildJSON(s);
}
}
The buildJson() method is simply :
protected JSONObject buildJSON(String jsonRaw){
JSONObject json = null;
try{
json = new JSONObject(jsonRaw);
}catch (Exception e){
e.printStackTrace();
}
return json;
}
But after, to know if the streamer is streaming, a part of the JSON string is nulled like that :
If the streamer is streaming, you will get the fist JSON I wrote on the top of that post, else you will get something like that :
{
"_links":{
"self":"https://api.twitch.tv/kraken/streams/bibixhd",
"channel":"https://api.twitch.tv/kraken/channels/bibixhd"
},
"stream":null
}
What I want to do is getting the "stream" part in another instance variable to either recover infos about the stream or to display an offline message.

What i have got from your OP is that the channelStatusObject return you the JSONObject which contains stream key. Now what you want to check is that whether this stream is null or not. So you could do something like :-
protected JSONObject checkStream(JSONObject parentObject){
JSONObject json = (JSONObject) parentObject.get("stream");
return json; // will return null if stream is null else stream JSONObject
}
You can use this method & check its return value to see if stream is null or not.

Related

How to set JSONObject to textview

I have been trying to set textview to "name" value from the json array responds but not working but if I run this code
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//view_member__progressBar.setVisibility(View.GONE);
try {
JSONObject object = new JSONObject(s);
detail.setText(object.getString("customer"));
} catch (JSONException e) {
e.printStackTrace();
}
}
I will get this result
{"name":"Adene Jonah","address":"Mr. John Doe 34 Tokai, Abuja. 7999.","util":"Demo Utility","minimumAmount":"500"}
but if I run this code below I will get nothing
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//view_member__progressBar.setVisibility(View.GONE);
try {
JSONObject object = new JSONObject(s);
JSONArray ar = new JSONArray();
ar =object.getJSONArray("customer");
detail.setText(ar.getString(0).);
} catch (JSONException e) {
e.printStackTrace();
}
}
this is the value of s
{"status":"00","message":"OK","access_token":"22071108062392","customer":{"name":"Adene Jonah","address":"Mr. John Doe
34 Tokai, Abuja. 7999.","util":"Demo
Utility","minimumAmount":"500"},"response_hash":"4a0d9cf3a7d63c5b0f9087931bef5540a0665add"}
First of all, there is no json array inside your json content. Json array is an array inside the json content. No such array exists in your json content. To give an example, assuming the name field is inside an array. It should have looked like this then:
{
"name": [
{
"personName": "Adene Jonah"
},
{
"personName": "abc"
}
],
"address": "Mr. John Doe 34 Tokai, Abuja. 7999.",
"util": "Demo Utility",
"minimumAmount": "500"
}
A json array starts with a [ sign and actually encloses many arrays. So if we give an example on the name field, it can contain many names. But your json content is a pure json object. You can check this from sites like these. JSON Beautifier

How to return a list of JSONObject as REST API response

I am creating a REST API using Spring Boot and using org.json for parsing data retrieved from another different service. From this service I am getting JSON data like in following format
{
"my_data":[
{
"user_data":{
"first_name":"FirstTest1",
"last_name":"LastTest1",
"age":"25"
}
},
{
"user_data":{
"first_name":"FirstTest2",
"last_name":"LastTest2",
"age":"35"
}
},{
"user_data":{
"first_name":"FirstTest3",
"last_name":"LastTest3",
"age":"45"
}
}
],
"count":10,
"is_safe":false
}
and I have to transform received data to the following JSON
[
{
"user_data":{
"first_name":"FirstTest1",
"last_name":"LastTest1",
"age":"25"
}
},
{
"user_data":{
"first_name":"FirstTest2",
"last_name":"LastTest2",
"age":"35"
}
},{
"user_data":{
"first_name":"FirstTest3",
"last_name":"LastTest3",
"age":"45"
}
}
]
I know I can use a POJO to map the data and send it (already doing this) but here the issue is that the data received from another service is not fixed e.g. it may or may mot have "first_name" or may have a different field like "country". So, in this situation I can not make POJO beforehand.
After going through some online resources I made some changes and my POST Controller method looks like this.
#PostMapping(path = "/searchusersdata")
public RETURN_SOMETHING searchUsersData(#RequestBody Map<String, String> searchData) {
List<JSONObject> finalDataCollection = new ArrayList<JSONObject>();
//Making some REST API CALL TO GET 'response' using 'searchData'
String someResponse = response.getBody();
JSONObject object = null;
try {
object = new JSONObject(someResponse);
} catch (JSONException e) {
e.printStackTrace();
}
String my_data= object.get("my_data").toString();
JSONArray intermediateJA = null;
intermediateJA = new JSONArray (my_data);
for(int i = 0; i < intermediateJA.length(); i++) {
JSONObject item = intermediateJA.getJSONObject(i);
if (item.keySet().contains("user_data"))
{
Object value = item.get("user_data");
finalDataCollection.add(new JSONObject(value));
}
}
//WHAT TO RESTURN HERE
}
Now, I don't know what to return hare. For a single JSONObject we can use return new ResponseEntity<>(return_data.toMap(), HttpStatus.OK); but for a collection I don't know. I am open to suggestion if I have to do it in entirely different way. I also know that with gson or jackson it might be easier but I have to use org.json.
instead of List , use JsonArray and use ResponseEntity to return it.
Example
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject = new JSONObject();
jsonArray.put(jsonObject);
return new ResponseEntity( jsonArray.toString(), HttpStatus.OK);

Any method of passing the Json data in URL format using JSONObject?

I create a java URL class which contain my Json data and have some function to obtain back my json data for doing some data comparison, I found out it's might not support by JSONObject for passing the data into the JSONObject. Do I need to use JSONArray in my case because my JSON data have array structure as well?
try
{
JSONObject obj = new JSONObject ();
obj.readJsonFromUrl(theUrl);
System.out.println(obj.toString());
}
catch(MalformedURLException e)
{
System.out.print("your problem here ...1");
}
}
else
{
System.out.print("Can't Connect");
}
I am sure that this is the place give me the error message because it return me this error in my compiler
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method readJsonFromUrl(URL) is undefined for the type JSONObject
there are also some warning message for that the JSONObject readJsonFromUrl method
private static JSONObject readJsonFromUrl(URL theUrl) throws IOException, JSONException {
Anyone can provide me the explaination of how the JSON data work in java? I saw quite number of Java class for JSON which make me confuse for it such as JSONObject, JSONArray , JSONValue. I search some information online but I also not very clear about it since I am very new to JSON data processing This is my sample json data and the data I need is scan_result only
{
"data_id":"a71a3c2588c6472bb4daea41a0b58835",
"file_info":{
"display_name":"",
"file_size":242,
"file_type":"Not available",
"file_type_description":"Not available",
"md5":"aa69ba384f22d0dc0551ace2fbb9ad55",
"sha1":"09ceb54e65df3d3086b222e8643acffe451a6e8a",
"sha256":"dcb46d6ae2a187f789c12f19c44bbe4b9a43bd200a3b306d5e9c1fcf811dc430",
"upload_timestamp":"2016-11-18T09:09:08.390Z"
},
"process_info":{
"blocked_reason":"",
"file_type_skipped_scan":false,
"post_processing":{
"actions_failed":"",
"actions_ran":"",
"converted_destination":"",
"converted_to":"",
"copy_move_destination":""
},
"profile":"File scan",
"progress_percentage":100,
"result":"Allowed",
"user_agent":""
},
"scan_results":{
"data_id":"a71a3c2588c6472bb4daea41a0b58835",
"progress_percentage":100,
"scan_all_result_a":"No Threat Detected",
"scan_all_result_i":0,
"scan_details":{
"Ahnlab":{
"def_time":"2016-11-08T15:00:00.000Z",
"location":"local",
"scan_result_i":0,
"scan_time":1,
"threat_found":""
},
"Avira":{
"def_time":"2016-11-08T00:00:00.000Z",
"location":"local",
"scan_result_i":0,
"scan_time":133,
"threat_found":""
},
"ClamAV":{
"def_time":"2016-11-08T10:28:00.000Z",
"location":"local",
"scan_result_i":0,
"scan_time":94,
"threat_found":""
},
"ESET":{
"def_time":"2016-11-08T00:00:00.000Z",
"location":"local",
"scan_result_i":0,
"scan_time":38,
"threat_found":""
}
},
"start_time":"2016-11-18T09:09:08.405Z",
"total_avs":4,
"total_time":250
},
"vulnerability_info":{
}
}
As mentioned here, there are many ways to solve this. Either you have to implement the read, parse operations yourself (#Roland Illig 's answer)
//you have to implement the readJSON method
InputStream is = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
return json;
} finally {
is.close();
}
Or you could use a library. The most well-known and widely used libraries are jackson and gson.
The big picture is that you try to "map" your json Object to a class.
You have your json file:
{
"id":1,
"name":"eirini",
"hobbies":["music","philosophy","football"]
}
and a class that represents this file and will store the values (depending on the library that you use there might be different requirements, for example getters, setters etc..)
public class Person {
public int id;
public String name;
public List<String> hobbies = new ArrayList<String>();
public String toString() {
return name +" has the id: " + id + " the following hobbies" + hobbies.get(0) + " " + hobbies.get(2);
}
}
Finally in your main method:
public static void main(String[] args) throws IOException, ParseException {
ObjectMapper mapper = new ObjectMapper();
InputStream input = this.getClass().getResourceAsStream(FILE); //read your file. There are many ways to achieve this.
ObjectMapper mapper = new ObjectMapper(); // just need one
Person eirini = mapper.readValue(input, Person.class);
System.out.println(eirini.toString());
You cannot pass json in url, you can pass it in body. Writing Json to stream body and post it using regular java method.
Here is oracle community url of explanation of your problem.
Required Jar can be downloaded from here.
Test Code Follows:
URL url = new URL("https://graph.facebook.com/search?q=java&type=post");
try (InputStream is = url.openStream();
JsonReader rdr = Json.createReader(is)) {
JsonObject obj = rdr.readObject();
JsonArray results = obj.getJsonArray("data");
for (JsonObject result : results.getValuesAs(JsonObject.class)){
System.out.print(result.getJsonObject("from").getString("name"));
System.out.print(": ");
System.out.println(result.getString("message", ""));
System.out.println("-----------");
}
}

How do I get data from JSON string?

Here is my code:
try {
JSONObject json = (JSONObject) new JSONTokener(result).nextValue();
System.out.println(json);
JSONObject json2 = json.getJSONObject("data");
String test = json2.getString("headline");
System.out.println(test);
} catch (JSONException e) {
e.printStackTrace();
}
My String values start with the object data. So I am trying to get that object first and then capture the the object headline inside that.
My problem is, it is not taking the object data from the string.
Once I reach the line JSONObject json2 = json.getJSONObject("data");, it throws the exception. Please shed some light on this.
"data": [
{
"headline": "Close Update"
"docSource": "MIDNIGHTTRADER",
"source": "MTClosing",
"dateTime": "2015-10-23T16:42:46-05:00",
"link": "Markets/News",
"docKey": "1413-A1067083-1B14K77PVTUM1O7PCAFMI3SJO4",
},
The value for the key data is a JSON array containing one object, and not an object itself.
To get that object inside data, replace your line that throws an exception with the following:
JSONObject json2 = json.getJSONArray("data").get(0);
This gets the data array as a JSONArray object and then gets the 0th element, which is the object you want.
Your data "object", isn't actually an object, it's an array, notice the opening square bracket... I'm assuming in your actual code, it closes with one too.
"data": [{
"headline": "Close Update"
"docSource": "MIDNIGHTTRADER",
"source": "MTClosing",
"dateTime": "2015-10-23T16:42:46-05:00",
"link": "Markets/News",
"docKey": "1413-A1067083-1B14K77PVTUM1O7PCAFMI3SJO4",
}]
Try json.getJSONArray("data")[0] instead... or whatever index you need
try {
JSONObject json = (JSONObject) new JSONTokener(result).nextValue();
System.out.println(json);
JSONObject json2 = json.getJSONArray("data")[0];
String test = json2.getString("headline");
System.out.println(test);
}
catch (JSONException e) {
e.printStackTrace();
Your problem is based on the fact that your service returns and array instead of a single json object, so from here you can follow this suggestions to process directly from the JSONArray Can't access getJSONArray in java, or, at server side you can encapsulate your response array into another object like this (java example):
public class Data<T> {
private List<T> elements;
public ObjectSugetionsDTO(){
And build the response like this:
return new ResponseEntity<Data<YourInternalRepresentation>>(
new Data<YourInternalRepresentation>(yourMethodCallForTheArray()),
HttpStatus.OK);
I have found the second way to be better at keeping my API cleaner and more readable
EDIT: Better way
I whould also suggest the use of retrofit (http://square.github.io/retrofit/), by doing so, your service calls is resumed to (Example of calling and API that retrieves a list of users):
public class UserService {
public static IUserService getUserService() {
return RestAdapterManager.createService(IUserService.class );
}
public interface IUserService{
#GET("/api/users")
public void getAllUsers(Callback<List<User>> callback);
}
}
and the service call itself
UserService.getUserService().getAllUsers(new Callback<List<User>>() {
#Override
public void success(List<User> users, Response response) {
Log.d("Exito! " , "" + users.size());
}
#Override
public void failure(RetrofitError error) {
Log.d("Fail!", error.getUrl());
}
});
The simple inicialization of the connection object
public static <S> S createService(Class<S> serviceClass, String username, String password) {
RestAdapter.Builder builder = new RestAdapter.Builder()
.setEndpoint(API_BASE_URL);//Your api base url
RestAdapter adapter = builder.setLogLevel(RestAdapter.LogLevel.FULL).build(); //change the logging level if you need to, full is TOO verbose
return adapter.create(serviceClass);
}

GSON identifying JSON Object as Primitive

I am writing a relatively simple messaging app that saves its logs in the JSON format, and I am using the GSON library to parse these. I load a JSON file from a server, and put it trough Gson.toJsonTree() function. I'm not sure this is expected, but when I test the result from the previous function with the isJsonSomething() functions (isJsonObject,isJsonAray,isJsonNull,isJsonPrimitive), isJsonPrimitive returns true, and I can't parse it into a object. This is my JSON file's contents:
{
"users": [
{
"picture": "",
"type": "user",
"name": "kroltan"
}
],
"description": "No description",
"messages": [
{
"content": "something",
"time": "2013-08-30 00:38:17.212000",
"type": "message",
"author": "someone"
}
],
"type": "channel",
"name": "default"
}
And here is the class used to parse it into POJOs: (CLEANUP comments is where I've removed irrelevant code from the post)
package com.example.testapp;
//CLEANUP: All needed imports
import com.example.testapp.data.*;
import com.google.gson.*;
public class JSONConverter {
public interface JsonTypeLoadedListener {
public void onSucess(JSONType jsonType);
public void onFailure(Exception e);
}
public static final String DATE_FORMAT = "dd-MM-yyyy HH:mm:ss.SSS";
public static final HashMap<String, Class<?>> JSON_TYPES = new HashMap<String, Class<?>>();
public JSONConverter() {
JSON_TYPES.clear();
JSON_TYPES.put("channel", Channel.class);
JSON_TYPES.put("user", User.class);
JSON_TYPES.put("message", Message.class);
}
public void loadFromURL(final URL url, final JsonTypeLoadedListener listener) {
new Thread(new Runnable() {
#Override
public void run() {
JsonObject result = null;
Gson gson = new GsonBuilder().setDateFormat(DATE_FORMAT).create();
if (url.getProtocol().equals("http")) {
try {
String content = //Loads from a server, omitted for clarity
result = gson.toJsonTree(content).getAsJsonObject();
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
listener.onFailure(e);
return;
}
} else if (url.getProtocol().equals("file")) {
try {
String content = //Loads from a file, omitted for clarity
result = gson.toJsonTree(content).getAsJsonObject();
br.close();
} catch (Exception e) {
e.printStackTrace();
listener.onFailure(e);
return;
}
}
listener.onSucess((JSONType) gson.fromJson(result, JSON_TYPES.get(result.get("type").getAsString())));
}
}, "URLLoader").start();
}
public JSONType loadFromString(String s) {
Gson gson = new Gson();
JsonObject result = gson.toJsonTree(s).getAsJsonObject();
return (JSONType) gson.fromJson(result, JSON_TYPES.get(result.get("type").getAsString()));
}
}
The classes Message, User and Channel all inherit from JSONType (a custom class with a field called type and some utility methods) and contain all values present in the above mentioned JSON file.
When it reaches gson.toJsonTree(content).getAsJsonObject(), I get this error in Logcat (string omitted for clarity, it's just the full file):
java.lang.IllegalStateException: Not a JSON Object: "String containing all the file with tabs represented as \t"
I'm guessing that the tabs are causing your issue. Try to remove them with:
content = content.replaceAll("\\s","")
this will simply clean your json string from any whitespace.
Btw I suggests you to get rid of Gson library and use directly the JSONObject provided in the android sdk. You can initialize it directly with the json string, as new JSONObject(content). :)

Categories