Looping over an array with alternating elements - java

Difficult to sum up the question in one sentence. It's easier to show you what I want. I have the following response of a Solr query:
{
.....
},
"facet_counts":{
"facet_queries":{},
"facet_fields":{
"city":[
"New York",23258,
"Los Angeles",13322,
"Paris",1189]},
"facet_ranges":{},
"facet_intervals":{},
"facet_heatmaps":{}}}
I use GSON to parse this JSON data and get a JsonArray containing the array "city". That contains alternating elements for the field name (key) and the corresponding value. So e.g. we have 13322 hits for "Los Angeles". I want to iterate over this array to get out the key=value pairs it contains.
I can think of a number of simple solutions for this task, for example
boolean isKey = true;
String key;
String val;
for(JsonElement je : facetMT) {
if(isKey) {
key = je.getAsString();
isKey = false;
} else {
val = je.getAsString();
resultMap.add(key,val);
isKey = true;
}
}
Or any other way to distinguish the odd and even elements, like checking for divisibility by 2.
But that feels crude. I'm sure Java has some other, elegant way to do this, like working with an iterator and skipping every other element. But I'm not good enough in Java to know how to do that, or any other supersmart way.
Any suggestions, or should I just go with the crude code?

If you're sure about the length of the array then you should read twice at one:
for(int i = 0; i < facetMT.size() - 1; i = i + 2) {
JsonElement key = facetMT.get(i).getAsString();
JsonElement value= facetMT.get(i + 1).getAsString();
resultMap.add(key,val);
}
Note the i < facetMT.size() - 1 condition to avoid IndexOutOfBoundsException

If you are sure that array will always have even amount of elements you can use two of them in one iteration like
Iterator<JsonElement> it = facetMT.iterator();
while (it.hasNext()) {
// iterate over two elements
resultMap.put(it.next().getAsString(), it.next().getAsString());
}
But to be honest proper answer would be enforcing results in form of array of objects like
"city":[
{"cityName":"New York", "amount":23258},
{"cityName":"Los Angeles", "amount":13322},
{"cityName":"Paris", "amount":1189}
]
instead of what you have now
"city":[
"New York",23258,
"Los Angeles",13322,
"Paris",1189
]
This way your code could look like:
for (JsonElement jsonElement : facetMT) {
JsonObject obj = (JsonObject)jsonElement;
resultMap.put(obj.get("cityName").getAsString(),
obj.get("amount").getAsString());
}

In this case use for loop with indexes
For(int i=0;i<...
FaceMT[i]= altered value.
Alterate value doesn't work with iterator for

Related

How to remove empty objects from Json Array

So the json is something like it,
"stores": [
{
"amazon": []
},
{
"flipkart": {
"product_store": "Flipkart",
"product_store_logo": "http://images-api.datayuge.in/image/ZmxpcGthcnRfc3RvcmUucG5n.png",
"product_store_url": "https://price-api.datayuge.com/redirect?id=aHR0cHM6Ly9kbC5mbGlwa2FydC5jb20vZGwvbWktYTEtYmxhY2stNjQtZ2IvcC9pdG1leDl3eHh6M2FtamF0P3BpZD1NT0JFWDlXWFVTWlZZSEVUJmFmZmlkPWFydW5iYWJ1bA",
"product_price": "14999",
"product_offer": "",
"product_color": "",
"product_delivery": "3-4",
"product_delivery_cost": "0",
"is_emi": "1",
"is_cod": "1",
"return_time": "10 Days"
}
},
{
"snapdeal": []
}
]
So the non empty object like flipkart is a JsonObject but all other empty objects are array. So I am so confused about how to remove them.
JSONArray store_array = product_details_json.getJSONObject("data").getJSONArray("stores");
for (int i = 0; i<store_array.length(); i++){
JSONObject store = store_array.getJSONObject(i);
if (!store.getJSONObject(store.keys().next()).has("product_store")){
store_array.remove(i);
}else {
Log.i("Size :",store_array.length()+"");
}
}
But that's not working. I know I am doing this all wrong. Because it has both array and objects so i get the following error
Value [] at amazon of type org.json.JSONArray cannot be converted to JSONObject
Need Help!
I see two problems with your code:
Your JSON structure for "stores" is heterogeneous — some elements have a key that maps to an array and some to an object. That's the immediate cause of the error you are seeing. You can either modify your JSON so everything key maps to an object or code defensively.
When you remove an entry, all subsequent entries move up one space, but since you then increment the loop index i, you skip the entry that just moved into the index you just removed. The easiest way to deal with that is to iterate through store_array in reverse order.
Putting this all together (and assuming you aren't going to change your JSON structure), something like the following (untested) should work:
JSONArray store_array = product_details_json.getJSONObject("data").getJSONArray("stores");
for (int i = store_array.length() - 1; i >= 0; i--){
JSONObject store = store_array.getJSONObject(i);
Object storeData = store.get(store.keys().next());
boolean isValidStore = storeData instanceof JSONObject
&& ((JSONObject) storeData).has("product_store");
if (!isValidStore) {
store_array.remove(i);
}
}

Check if List <String> contains substring

I'm developing an Android app where I get a List of names (Strings) from a SQLite database. Then, I show them on a List of Cardviews.
I'm also given a substring, so I'd like to check if any of the elements of the original List contains that substring on it.
I'm having problems using LIKE in the SQLite query, so I've just fixed it with a try{ } catch (Exception e) { }, but I don't feel so comfortable with it.
Anyway, since I originally have all of the names stored in the List of Strings, I don't really need to fecth the new ones again from the database, I can just search them on the original List of Strings. But I don't know how.
For example, let's say the List of String has this 5 elements:
Hello
Wall
Helicopter
Chair
Hell
And I'm given the substring Hel. I should get the Hello, Helicopter and Hell strings, since all of them contain the substring Hel.
Any ideas on how to achieve this goal?
It's not that I want to fix it this way. I sincerely would like to know what's the most efficient, if retrieveing the data again from the database, or search it from the List of Strings.
EDIT: I originally said that I was using the CONTAINS query, but I missed the word. I just meant the LIKE query. I'm actually using it, and it works fine but I don't really know what's the most efficient in my case.
Thanks in advance.
Here is my suggestion
public boolean isSubstring(String str, String key) {
if(key.length() > str.length()) return false;
for(int i = 0; i <= str.length() - key.length(); i++) {
if( key.equals(str.substring(i, i + key.length())) ) return true;
}
return false;
}
public List<String> findStings(List<String> list, String key) {
List<String> newList = new ArrayList<>();
for(int i = 0; i < list.size(); i++) {
if(isSubstring(list.get(i), key)) {
newList.add(list.get(i));
}
}
return newList;
}
If you are working with Java 8 you can replace the for-loop of the second method as:
for(String str: list) {
if(isSubstring(str, key)) newList.add(str);
}
or simply as:
list.stream().filter((str) -> (isSubstring(str, key))).forEach((str) -> { newList.add(str); });
If you're using Java 8:
List<String> filteredList = myList.stream().filter(s -> s.contains("Hel")).collect(Collectors.toList());
If you're using anything earlier, use a for each loop:
List<String> filteredList = new ArrayList<String>();
for(String s : myList)
if(s.contains("Hel")
filteredList.add(s);

read nested json object using loop

I have been searching from a long time and no solutions is working for me.
I have to retrieve the values from the json object using some loop , and number of nested values is random these can be 1 or may be 10.
json looks like this :
{
"keyInfo":[
{
"name":"ipek",
"key":"1221"
},
{
"name":"ipek",
"key":"1221"
}
],
"terminalInfo":{
"dateExp":"2-2-2",
"deviceId":"1222",
"tid":"122"
}
}
I have tried alot of solutions one of them is this :
JSONObject jsonObject =new JSONObject(jsonString);
JSONObject jsonChildObject = (JSONObject)jsonObject.get("keyInfo");
Iterator iterator = jsonChildObject.keys();
String key = null;
while(iterator.hasNext()){
key = (String)iterator.next();
System.out.println("inval value: "
+ ((JSONObject)jsonChildObject.get(key)).get("inval"));
}
but none of them is working for me please help.
THANKS IN ADVANCE.
You are trying to use an array as a map. jsonChildObject is actually an JSONArray. It does not have keys; for instance you can have the same {"name":"ipek","key":"1221"} multiple times. If you know that the key is unique among items in this array, you can try and build a HashMap out of it if you need, but the structure you have is definitely an array.

Remove object from json response Android

Hello guys I need your help. How do I remove this from my json response?
[
{
"Woa": [
"Seo",
"Rikjeo",
"JDa"
]
},
"Aha",
"Aad",
"Char"
]
I want to remove this:
{
"Woa": [
"Seo",
"Rikjeo",
"JDa"
]
}
This is what I tried so far:
for (int i = 0; i < array.length(); ++i) {
list.add(array.getString(i));
}
list.remove(0);
But it is still not removed. How do I do that? Any ideas will be greatly appreciated
Edited list.remove(1) to (0)
After removing the item you need to create the JSON again using the list.
list.remove(1);
JSONArray jsArray = new JSONArray(list);
If you want to convert JSONArray to JSON string:
jsArray.toString()
Your list will be updated, not your JSON object.
Why don't you use a JSONparser.
JSONObject obj = new JSONObject(mystring);
obj.remove("entryname");
One problem is that the element you are trying to remove is the first one, and the index of the first element of a JSONArray object is zero.
You are calling list.remove(1) which removes the second element of the array.
The following should be sufficient:
list.remove(0);
... without the stuff prior to it.
If this is not working, then my guess is that you are removing the element too late; i.e. after the JSONArray object has been serialized. However, we need to see more (relevant) code to be sure.
The Android documentation for JSONArray fails to mention that array indexing is zero-based. However, it is. I checked the source code. Furthermore most other Java data structures (arrays, lists etcetera) use zero-based indexing. (A notable exception is Java data structures modelled on the W3C DOM APIs.)
You should make something that's more general so you don't have to modify your code EVERY time the JSON changes. For example:
//here we create the JSONArray object from the string that contains the json data
JSONArray array = new JSONArray(myJsonString);
//here we create a list that represents the final result - a list of Strings
List<String> list = new ArrayList<>();
//here we parse every object that the JSONArray contains
for (int i = 0; i < array.length(); i++) {
//if the current item is an JSONObject, then we don't need it, so we continue our iteration
//this check is not necessary because of the below check,
//but it helps you see things clearly: we IGNORE JSONObjects from the array
if (array.get(i) instanceof JSONObject) {
continue;
}
//if our current object is a String, we add it to our final result
if (array.get(i) instanceof String) {
list.add(array.getString(i));
}
}
you can parse your string to JSONArray, the first element of the array is what you want.
#Test
public void test() {
String str = "[ {\"Woa\": [\"Seo\",\"Rikjeo\",\"JDa\"]},\"Aha\",\"Aad\",\"Char\"]";
try {
JSONArray array;
array = new JSONArray(str);
System.out.println(array);
System.out.println(array.get(0));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
[{"Woa":["Seo","Rikjeo","JDa"]},"Aha","Aad","Char"]
{"Woa":["Seo","Rikjeo","JDa"]}
PASSED: test
You Can one by one remove
while (user_data.length() > 0) {
user_data.remove(user_data.keys().next());
}

Remove objects from a list through an array of integers?

Hi community I have a question, I happen to have an array of objects loaded on startup, through that generate array another array of integers that contains your code, it appears that array of integers'm removing their values, what I want is to compare the list of integer array currently have with the array of objects, and remove all code object that whole array mentioned is found.
My code java:
private List<ValidColumnKey> columnCustomer;
private int[] selectedCustomer;
public void init(){
this.setColumnCustomer(new ArrayList<ValidColumnKey>());
this.getColumnCustomer().add(new ValidColumnKey(1, "Codigo", "code"));
this.getColumnCustomer().add(new ValidColumnKey(2, "Nombre", "name"));
this.getColumnCustomer().add(new ValidColumnKey(3, "Nombre Comercial", "comercialName"));
this.getColumnCustomer().add(new ValidColumnKey(4, "Estado", "isActive"));
this.setSelectedCustomer(new int [this.getColumnCustomer().size()]);
int i = 0;
for(ValidColumnKey column : this.getColumnCustomer()){
this.getSelectedCustomer()[i] = column.getCodigo();
i++;
}
}
I mean I would have my array of integers with codes removed, like this:
selectedCustomer = [1, 2, 3];
What I wanted was to remove from the list of objects that do not have codes in the array of integers, but it is not my code:
List<ValidColumnKey> auxRemoColumnKeys = new ArrayList<ValidColumnKey>();
for(ValidColumnKey column : this.getColumnCustomer()){
for(Integer codigo : this.getSelectedCustomer()){
if (column.getCodigo() != codigo) {
auxRemoColumnKeys.add(column);
break;
}
}
}
this.getColumnCustomer().remove(auxRemoColumnKeys);
I could guide the solution.
this.getColumnCustomer().remove(auxRemoColumnKeys);
This statement assumes you have a valid equals method for your class ValidColumnKey, which I suspect is probably not the case.
What you want to do is iterate with a Iterator. Some sample code could be like
Set<Integer> toRemoveCodes = new HashSet<Integer>(Arrays.asList(1, 2, 3));
for (Iterator<ValidColumnKey> it = this.getColumnCustomer().iterator(); it.hasNext(); ) {
ValidColumnKey curColumnKey = it.next();
Integer code = curColumnKey.codigo();
if (toRemoveCodes.contains(code)) {
it.remove();
}
}
There are multiple reasons your current attempt is failing. The first is that this line:
if (column.getCodigo() != codigo) {
Is testing for object equivalence between Integers, not value equavalence between ints. If you want to compare Integers, you have to use the equals method:
if (!column.getCodigo().equals(codigo)) {
However, if getCodigo returns an int and getSelectedCustomer returns an int[] then this line should be changed instead:
for(int codigo : this.getSelectedCustomer()){
Because you didn't need to use Integer in the first place.
Secondly, this line attempts to remove auxRemoColumnKeys itself so you probably mean removeAll:
this.getColumnCustomer().remove(auxRemoColumnKeys);
Lastly, your logic is generally flawed. It basically says "for each element in getColumnCustomer, if getCodigo is not equal to all of getSelectedCustomer remove it". I don't think that's what you've intended.
This is a modified loop that uses the same "add to a list and remove the list items" procedure but the logic will work:
List<ValidColumnKey> auxRemoColumnKeys = new ArrayList<ValidColumnKey>();
int[] selected = this.getSelectedCustomer();
for (ValidColumnKey column : this.getColumnCustomer()) {
int i = 0;
for ( ; i < selected.length; i++) {
/* note: if getCodigo returns an Integer change this check to
* "if (column.getCodigo().equals(selected[i])) {"
*/
if (column.getCodigo() == selected[i]) {
break;
}
}
/* this says "if the search loop did not break early" */
if (i == selected.length) {
auxRemoColumnKeys.add(column);
}
}
this.getColumnCustomer().removeAll(auxRemoColumnKeys);

Categories

Resources