Skip to content
Advertisement

Trying to search through JSON in Java

I am using the Marvel API to try and get the names of heroes based on what the user has typed inside of the search box. The search functionality works I am pretty sure as it will search through a regular array however I am trying to make it search through a JSONArray. Below is what I have so far.

try {
            //This is the input stream section. No idea why these need to be here but they are
            //in = input
            InputStream in = new BufferedInputStream(urlConnection.getInputStream());
            //bin = buffered input.... i think.
            BufferedReader bin = new BufferedReader(new InputStreamReader(in));
            //temp string to hold each line that's read from the reader
            String inputLine;

            //this keeps adding the lines to the string builder
            while ((inputLine = bin.readLine()) != null) {
                sb.append(inputLine);
            }

            //putting the string builder into a json object
            JSONObject jsonObject = new JSONObject(sb.toString());

            //Checking if the jsonObject has a response inside of it.
            //If there is one however it is false then no results are found.

            if (jsonObject.has("Response") && jsonObject.getString("Response").equals("False")){
                error = true;
            } else{
                JSONArray results = jsonObject.getJSONArray("name");
            }

This is the part I am pretty sure is the problem because I do not think it is going to the part of the JSON which has the name inside of it. There is an example JSON below where the results are when the user searches the word “captain”

object      {7}
    code    :   200
    status  :   Ok
    copyright   :   © 2020 MARVEL
    attributionText :   Data provided by Marvel. © 2020 MARVEL
    attributionHTML :   <a href="http://marvel.com">Data provided by Marvel. © 2020 MARVEL</a>
    etag    :   75d3eb0f8a6fd4ce06372a8e382af0fe85ea966c
    data        {5}
         offset :   0
         limit  :   10
         total  :   19
         count  :   10
         results        [10]
           0        {11}
                      id    :   1009220
                      name  :   Captain America
                      description   :   Vowing to serve his country any way he could, young Steve Rogers 
                      took the super soldier serum to become America's one-man army. Fighting for the 
                      red, white and blue for over 60 years, Captain America is the living, breathing 
                      symbol of freedom and liberty.
                      modified  :   2020-04-04T19:01:59-0400
                      thumbnail     {2}
                      resourceURI   :   http://gateway.marvel.com/v1/public/characters/1009220
                      comics        {4}
                      series        {4}
                      stories       {4}
                      events        {4}
                      urls      [3]
                      1     {11}
                      2     {11}
                      3     {11}
                      4     {11}
                      5     {11}
                      6     {11}
                      7     {11}
                      8     {11}
                      9     {11}

I need to know why it won’t only add the name part of the JSON to the JSONArray. All that happens currently is that when the user searches something nothing appears when trying it this way. Any help is appreciated.

Advertisement

Answer

If I’m understanding this correctly, the name field is a string, which is an element of the results array, which is itself a child of the data JSON object. To access the results array, try:

JSONArray results = jsonObject.getJSONObject("data").getJSONArray("results")

You can then access the individual result name at index i like this:

String name_i = results.getJSONObject(i).getString("name")

Hope this helps.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement