I am trying to parse a REST API that has 3 possible variations. The first one is the one I already have working where there is an array for the "row" output as shown in the photo below.
There is also row as an object, as pictured below.
And finally, one where there is no data, as shown below.
Any one of these is a possible output when parsing the API, I need something like an if statement to see which one is the output and parse the information from there, but I do not know how to create an if statement for this particular need. Here is my current code:
JSONObject parentObject = new JSONObject(finalJson);
JSONObject responseJSON = JSONUtils.jsonObjectFromJSONForKey(parentObject, "response");
if (responseJSON != null) {
JSONObject resultJSON = JSONUtils.jsonObjectFromJSONForKey(responseJSON, "result");
JSONObject contactsJSON = JSONUtils.jsonObjectFromJSONForKey(resultJSON, "Potentials");
JSONArray parentArray = contactsJSON.getJSONArray("row");
List<Photoshoots> photoshootsList = new ArrayList<>();
for (int i = 0; i < parentArray.length(); i++) {
JSONObject mainObject = parentArray.getJSONObject(i);
JSONArray mainArray = mainObject.getJSONArray("FL");
Photoshoots photoshoot = new Photoshoots();
...
I have solved the problem I was having. It may not be the ideal way to solve the problem, but it does work. Inside of the doInBackground in AsyncTask, I had initially converted the JSON result data into a StringBuffer, then converted the StringBuffer to a String. I then used one if and 2 else if statements to parse the data. The first if statement checks if the JSON string contains the phrase "nodata" representing that no data matches my original criteria, shown below:
if (finalJson.contains("nodata"))
I then had it pass on null data to be checked in onPostExecute for later. Next else if statement checked to see if row is an array. The way I did that was check to see if the string had a [ after row, indicating that it was an array, shown below:
else if (finalJson.contains("{\"response\":{\"result\":{\"Potentials\":{\"row\":["))
I then parsed the information as follows:
JSONArray parentArray = contactsJSON.getJSONArray("row");
for (int i = 0; i < parentArray.length(); i++) {
JSONObject mainObject = parentArray.getJSONObject(i);
JSONArray mainArray = mainObject.getJSONArray("FL");
the final else if, which could probably just be an else statement, is almost the same, except looking for a { after row instead of [, indicating it was just an object, as shown below:
else if (finalJson.contains("{\"response\":{\"result\":{\"Potentials\":{\"row\":{"))
I then parsed the information as follows:
JSONObject mainObject = contactsJSON.getJSONObject("row");
JSONArray mainArray = mainObject.getJSONArray("FL");
Inside of the onPostExecute, I have a code that will run a class that extends ArrayAdapter for the results contained in both of the else if statements, but I don't want it to run if there is no data, so I did the following:
protected void onPostExecute(List<Photoshoots> result) {
super.onPostExecute(result);
if (result != null) {
PhotoshootAdapter adapter = new PhotoshootAdapter(getApplicationContext(), R.layout.row, result);
lvPhotoshoots.setAdapter(adapter);}
else{
new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
btnRefresh.setText("No Data to Show. Try again in " + millisUntilFinished / 1000 + " Seconds");
}
public void onFinish() {
btnRefresh.setText("Refresh");
btnRefresh.setEnabled(true);
}
}.start();
The result will pass the null data if there is nodata, but a list if there is data within the result. Like I said, there may be a better way to solve this problem, but the app works now and it doesn't crash regardless of the data that is returned.
Related
I have table named testTable( in sql server 2014) and i have one column called holidays inside it , in my case i have two values inside this column for example (2018-03-02 and 2018-04-02) and i use this method for getting this value and then puttinh it in json Object here is my code:
public static JSONArray convertToJSON(ResultSet resultSet) throws Exception {
JSONArray jsonArray = new JSONArray();
JSONObject obj = new JSONObject();
while (resultSet.next()) {
int total_rows = resultSet.getMetaData().getColumnCount();
for (int i = 0; i < total_rows; i++) {
jsonArray.put(obj.put(resultSet.getMetaData().getColumnLabel(i+1)
.toLowerCase(), resultSet.getObject(i+1)));
}
}
System.out.println(jsonArray);
return jsonArray;
}
but when i run this code i got only last data written two times in my array it will look like this :
[{"startDate":2018-03-02},{"startDate":2018-03-02}]
but it should look like this:
[{"startDate":2018-03-02},{"startDate":2018-03-03}]
I mean it writes last data twice but when i try to print it in console i got right values?
what should i change to get all startDate values properly?
You are overwriting the record by doing the following operation
jsonArray.put(obj.put(resultSet.getMetaData().getColumnLabel(i+1)
.toLowerCase(), resultSet.getObject(i+1)));
While putting the object your key is startdate which gets overwritten
obj.put(resultSet.getMetaData().getColumnLabel(i+1)
.toLowerCase(), resultSet.getObject(i+1))
Also, there is something wrong with your logic. The following code will give you column count and NOT the row count
int total_rows = resultSet.getMetaData().getColumnCount();
You can use the following snippet to get the object:
public static JSONArray convertToJSON(ResultSet resultSet) throws Exception {
JSONArray jsonArray = new JSONArray();
while (resultSet.next()) {
JSONObject obj = new JSONObject();
obj.put("startDate" , resultSet.getString('holidays'))
jsonArray.put(obj);
}
return jsonArray;
}
The arraylist should be of the form, [{"12345"}, {"67890"}, etc...]
But when I close my app - I mean press the back button the amount of times it takes to get back to the Android home screen - and then restart it, I see MatchingContactsAsArrayListis[{"12345"}, {"67890"}, etc...,{"12345"}, {"67890"}, etc...]
If I close it twice, the arraylist comes up 3 times and so on, it keeps getting longer. It should just display each value once.
I thought editorMatchingContactsAsArrayList.remove(jsonMatchingContactsAsArrayList).commit();
would take care of this.
Here's my code:
#Override
public void onResponse(String response) {
//convert the JSONArray, the response, to a string
String MatchingContactsAsString = response.toString();
//make an arraylist which will hold the phone_number part of the MatchingContacts string
MatchingContactsAsArrayList = new ArrayList<String>();
try {
JSONArray Object = new JSONArray(MatchingContactsAsString);
for (int x = 0; x < Object.length(); x++) {
final JSONObject obj = Object.getJSONObject(x);
MatchingContactsAsArrayList.add(obj.getString("phone_number"));
}
SharedPreferences sharedPreferencesMatchingContactsAsArrayList = PreferenceManager.getDefaultSharedPreferences(getApplication());
SharedPreferences.Editor editorMatchingContactsAsArrayList = sharedPreferencesMatchingContactsAsArrayList.edit();
Gson gsonMatchingContactsAsArrayList = new Gson();
String jsonMatchingContactsAsArrayList = gsonMatchingContactsAsArrayList.toJson(MatchingContactsAsArrayList);
editorMatchingContactsAsArrayList.putString("MatchingContactsAsArrayList", jsonMatchingContactsAsArrayList);
editorMatchingContactsAsArrayList.remove(jsonMatchingContactsAsArrayList).commit();
editorMatchingContactsAsArrayList.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
SharedPreferences.remove() method takes in the key that you used to save.
Which in this case is "MatchingContactsAsArrayList".
And actually, you don't need to use remove(), because putString() with an existing key, will override that value. Please make sure that the data from response is correct.
Is it possible to change the name of a Json property without serialization with Gson? For example, given this Json
{
"1": {
...
},
"2": {
...
}
}
could I change the "1" to a "3" without removing its contents. I know that the addProperty method adds a new property, or overwrites an existing property with a new value, but I want to change the name of a property without affecting its value. Also, pasting the existing value as the second argument of addProperty will not suffice.
EDIT: To add more context, I will explain the bigger picture. I have a JSON string that is a couple thousand lines long. I'm writing a program leveraging Gson in order to change the values in that JSON string. I am at a point where I not only want to change the values of properties, but the names of the properties themselves. I have done everything so far without serialization.
Here is a snippet of the Java I wrote:
String file = "\\temp.json";
FileReader reader = new FileReader(file);
JsonStreamParser parser = new JsonStreamParser(reader);
// Parse entire JSON
JsonElement element = parser.next();
// Get root element
JsonObject sites = element.getAsJsonObject();
// Get first child element
JsonObject site1 = sites.getAsJsonObject("1");
JsonObject clust1 = site1.getAsJsonObject("CLUST");
for(int i = 1; i < 12; i++) {
// "Dynamic" variable
String num = Integer.toString(i);
// Get property whose name is a number, has siblings
JsonObject one = custCluster1.getAsJsonObject(num);
one.getAsJsonObject().addProperty("name", "cluster" + i);
JsonObject subOne = one.getAsJsonObject("SUB");
subOne.getAsJsonObject().addProperty("name", "aName" + i);
for(int n = 1; n < 1002; n++) {
// "Dynamic" variable
String inst = Integer.toString(n);
// Get property whose name is a number, has siblings
JsonObject subSub = subOne.getAsJsonObject(inst);
// If the property doesn't exist, then don't execute
if(subSub != null) {
JsonArray subSubArray = subSub.getAsJsonArray("SUBSUB");
subSub.getAsJsonObject().remove("name");
int m = 0;
while(m < subSubArray.size()) {
subSubArray.get(m).getAsJsonObject().remove("SR");
subSubArray.get(m).getAsJsonObject().remove("FI");
subSubArray.get(m).getAsJsonObject().remove("IND");
subSubArray.get(m).getAsJsonObject().addProperty("ST", "1");
subSubArray.get(m).getAsJsonObject().addProperty("ID", "2");
subSubArray.get(m).getAsJsonObject().addProperty("DESCR", "hi");
m++;
}
m = 0;
}
}
}
Thanks to #mmcrae for helping and suggesting this method.
Since I'm already saving the (key, value) pairs in variables, you can remove the property whose name you want to change from the parent, and then add it back with a new name and the content that was already saved.
Like this:
JsonObject sites = element.getAsJsonObject();
JsonObject site1 = sites.getAsJsonObject("1");
JsonObject clust1 = site1.getAsJsonObject("CLUST");
site1.remove("CLUST");
site1.add("NEWCLUST", clust1);
I have been fiddling with this for a few hours, essentially what I am trying to do is create a JsonObject and get it's value inside a for loop and Set it to a TextView. It is working however I am getting only the Second loop values, to clarify this is the output from my JsonObject
{"Areas":[{"City":"Dallas","State":"Texas"},{"City":"Seattle","State":"Washington"}]}
However I am only getting {"City":"Seattle","State":"Washington"} the second values in the loop inside my Textviews setText. I would like to get both the first and second loop inside my Textview.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mystates);
try {
// Create my Json Array
JSONObject newJsonObject = new JSONObject();
JSONArray myarray = new JSONArray();
JSONObject states= new JSONObject();
states.put("City", "Dallas");
states.put("State", "Texas");
JSONObject states2= new JSONObject();
states2.put("City", "Seattle");
states2.put("State", "Washington");
myarray.put(states);
myarray.put(states2);
newJsonObject.put("Areas", array);
// Ends Json Array
//set Textviews
TextView citiess = (TextView)findViewById(R.id.citiess);
TextView statess = (TextView)findViewById(R.id.statess);
String cityString="";
String stateString="";
// retrieve Json Array and set For Loop
JSONArray jaLocalstreams = names.getJSONArray("Areas");
for(int position=0;position<jaLocalstreams.length();position++)
{
JSONObject JO = jaLocalstreams.getJSONObject(position);
cityString= JO.getString("City");
stateString = JO.getString("State");
citiess.setText(""+cityString);
statess.setText(""+stateString);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
I have pretty much narrowed down the problem area and I think it is this
citiess.setText(""+cityString);
statess.setText(""+stateString);
setText is replacing the values with the newest ones which makes sense, how can I change this so that it does not do that? When I put this in the for loop
System.err.println(cityString+","+stateString);
I get both loop values of Dallas,Texas and Seattle,Washington. Which is why I am sure that setText is the issue any suggestions would be great.
Its seems that cityString,stateString get overwrite on loop and setText will print last value only.Therfore you need to append json values in string and then set those values after loop as
for (int position = 0; position < jaLocalstreams.length(); position++) {
JSONObject JO = jaLocalstreams.getJSONObject(position);
cityString += JO.getString("City");
stateString += JO.getString("State");
}
citiess.setText("" + cityString);
statess.setText("" + stateString);
I was using JSONParser to obtain results of a search, for that I followed this tutorial: http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
The thing is that, the API I am using gives the results like this:
{"response":[50036,{"aid":88131498,"owner_id":61775052,"artist":"Terror Squad","title":"Lean Back (OST Need For Speed Underground 2)","duration":249,"url":"http:\/\/cs4408.vkontakte.ru\/u59557424\/audio\/7f70f58bb9b8.mp3","lyrics_id":"3620730"},{"aid":106963458,"owner_id":-24764574,"artist":"«Dr. Dre ft Eminem, Skylar Grey (Assault Terror)","title":"I Need A Doctor (ASSAULT TERROR DUBSTEP REMIX)»","duration":240,"url":"http:\/\/cs5101.vkontakte.ru\/u79237547\/audio\/12cd12c7f8c2.mp3","lyrics_id":"10876670"}]}
My problem comes when I have to parse the first integer (here it is 50036) which is the number of results found.
I don't know how to read that integer.
This is my code:
private void instance(String artisttrack){
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
String jsonurl = new String( "https://api.vk.com/method/audio.search?access_token=ACC_TOKEN&api_id=ID&sig=SIG&v=2.0&q=" + artistname + artisttrack + "&count=5");
JSONObject json = jParser.getJSONFromUrl(jsonurl);
try {
// Getting Array of Contacts
response = json.getJSONArray(TAG_RESPONSE);
// looping through All Contacts
for(int i = 0; i < response.length(); i++){
JSONObject c = response.getJSONObject(i);
// Storing each json item in variable
//int results = Integer.parseInt(c.getString(TAG_RESULTS));
String aid = c.getString(TAG_AID);
String owner_id = c.getString(TAG_OWNER_ID);
String artist = c.getString(TAG_ARTIST);
String title = c.getString(TAG_TITLE);
String duration = c.getString(TAG_DURATION);
// Phone number is agin JSON Object
//JSONObject phone = c.getJSONObject(TAG_PHONE);
String url = c.getString(TAG_URL);
String lyrics_id = c.getString(TAG_LYRICS_ID);
Log.e("áaaaaaaaaaaaaaa", url);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
The JSONParser.java is like written in the tutorial.
And here 2 lines of the logcat error:
W/System.err(10350): org.json.JSONException: Value 50036 at 0 of type java.lang.Integer cannot be converted to JSONObject
W/System.err(10350): at org.json.JSON.typeMismatch(JSON.java:100)
Your JSON sample is a poor way to organize the results: mixing the number in with the result objects. Is the number supposed to indicate the number of objects in the array or something else?
If you can assume that this number will always be the first element, and according to this then it's supposed to work this way, you can try to read the first value of the array outside the loop:
response = json.getJSONArray(TAG_RESPONSE);
// from your example, num will be 50036:
num = response.getInt(0);
for (int i = 1; i < response.length(); i++){
JSONObject c = response.getJSONObject(i);
Note that the example in the linked documentation has this number as a string:
{"response":
["5",
{"aid":"60830458","owner_id":"6492","artist":"Noname","title":"Bosco",
"duration":"195","url":"http:\/\/cs40.vkontakte.ru\/u06492\/audio\/2ce49d2b88.mp3"},
{"aid":"59317035","owner_id":"6492","artist":"Mestre Barrao","title":"Sinhazinha",
"duration":"234","url":"http:\/\/cs510.vkontakte.ru\/u2082836\/audio\/
d100f76cb84e.mp3"}]}
But JSONArray.getInt() will parse the String as an int for you.
And notice that some of the values in the objects in your array are also numbers, you may want to read those as int also:
int aid = c.getInt(TAG_AID);
int owner_id = c.getInt(TAG_OWNER_ID);
int duration = c.getInt(TAG_DURATION);
A lot of the values you are trying to parse in are not String objects, specifically "aid", "owner_id", and "duration". Use the correct method to retrieve values. For example:
int aid = c.getInt(TAG_AID);
int owner_id = c.getInt(TAG_OWNER_ID);
String artist = c.getString(TAG_ARTIST);
String title = c.getString(TAG_TITLE);
int duration = c.getInt(TAG_DURATION);
edit: Another error that I missed is you start your array with 50036. This is not a JSONObject and cannot be parsed as so. You can add a conditional statement to check if it's array index 0 to parse the int using getInt(), and then parse as JSONObjects for the rest of the array values.
Try changing
response = json.getJSONArray(TAG_RESPONSE);
into
response = (JSONObject)json.getJSONArray(TAG_RESPONSE);
I dont have any experience with JSONObject, but works often with type mismatches.
Try putting 50036 in quotes like this "50036" .