Search engine on android

0

I'm trying to make a search engine in my android app. It would be something like this. I get the data in a json:

{
  "naturaleza": [
    {
      "especie": "flores",
      "array_tipo": [
        {
          "tipo": "rosa",
          "texto": "Las rosas son flores muy lindad. Representan el amor y la amistad para muchas personas."
        },
        {
          "tipo": "tulipan",
          "texto": "Los tulipanes son muy gustados por las personas. Los amantes los utilizan para seducir"
        },
        {
          "tipo": "margarita",
          "texto": "Laa margaritas son flores que huelen muy bien. Son pequeñas y adorman muchos jardines."
        }
      ]
    },
    {
      "especie": "animales",
      "array_tipo": [
        {
          "tipo": "leon",
          "texto": "El leon es le rey de la selva, es un animal muy fuerte y grane. Todos los demas animales le tiene miedo."
        },
        {
          "tipo": "tiburon",
          "texto": "El tiburon es el rey del mar. Nada muy rapido y es muy furte."
        },
        {
          "tipo": "perro",
          "texto": "El perro es el mejor amigo del hombre. Es parte de la familia, los niños los quieren mucho"
        }
      ]
    }
  ]
}

As you can see I have an array nature that has two elements (flowers and animals) that are array and at the same time each one has three elements more ...

Now, I want that when looking for the word "king" I am debunked by the position where it is located in the json, that is (position 1 in array nature, position 0 in array animals). It would be something like, when a word is written in the search bar in a listview it shows me the possible results:

the lion is the king of the jungle, it is an animal (1.0)

the shark is the king of the sea. Nothing very (1,1)

and when you select one this has saved its positions to be able to locate them in the json ..... If someone can help me ... thanks in advance ...

    
asked by Felix A Marrero Pentón 24.05.2017 в 18:43
source

2 answers

1

I'll give you an example that is not the most ideal for efficiency if you would have a very large json, but I could guide you a bit.

void buscarJson(String textoBuscar, String tuJson) {
    JSONObject array = null;
    try {
        array = new JSONObject(tuJson);
        JSONArray actual = array.getJSONArray("naturaleza");
        for (int i = 0; i < actual.length(); i++) {
            JSONArray sub = ((JSONObject)actual.get(i)).getJSONArray("array_tipo");
            for (int b = 0; b < sub.length(); b++) {
                if(((JSONObject)sub.get(b)).getString("texto").contains(textoBuscar))
                {
                    Log.e("json", "encontrado en: naturaleza " + i + " posicion " + b);
                }
            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

Simply get the array of nature , and parsea it as many times as elements it contains, each path in turn will parse the element array_type . Finally, check if text contains the string that is passed to you and, if so, you have the position of the nature array stored in the variable i > and in b the position within array_type

    
answered by 25.05.2017 в 11:15
0

Another option that you have is to create your object that we can call Being, which will have inside a String species and an Array of type. This array will have 2 String inside, I do not know if I've explained the truth very well ...

If you do this, when you deserialize it with the Gson class, it directly translates the json to objects, where it is much easier to go through them, access their properties and save their positions.

    
answered by 25.05.2017 в 15:01