0

I got my JSON string from my server which contains this values:

{"server_response":[{"violation":"Driving with No Helmet"},{"violation":"Try"}]}

What I'm trying to do is convert this JSON string into String Array or Arraylist with the values of Driving with no Helmet and Try and use it as options on an Autocomplete Textview. But I cant seem to convert them correctly. Any help or tips on what I should do? Currently I am getting the JSON String from another activity and passing it to the activity where it should be used using this:

String json_string2 = getIntent().getExtras().getString("json_data");

Anyone has time I'm willing to learn. :) Thanks

PS: Managed to get it working. @Suhafer's answer is perfect. Thanks to everyone for the warm help! :)

Ekko Sky
  • 39
  • 1
  • 1
  • 10

6 Answers6

2

I think first, you need to parse the json to get the list of string that you want:

String json_string2 = getIntent().getExtras().getString("json_data");
List<String> lStringList = new ArrayList<>();
try {
      JSONObject lJSONObject = new JSONObject(json_string2);
      JSONArray lJSONArray = lJSONObject.getJSONArray("server_response");
      for (int i = 0; i < lJSONArray.length(); i++)
      {
        lStringList.add(
           lJSONArray.getJSONObject(i).getString("violation"));
      }
}
catch(Exception e)
{
    e.printStackTrace();
}

Then, you need to set that list to your adapter:

ArrayAdapter<String> yourListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lStringList);

Next, implement the adapter to your AutoCompleteTextView.

lAutoCompleteTextView.setAdapter(lStringArrayAdapter);

Hope, that helps you.

Suhafer
  • 99
  • 4
  • So far this is the one I tried first and it worked perfectly! Sadly I cannot upvote your answer but It was a very great help. Thanks! :) – Ekko Sky Nov 03 '17 at 07:57
  • 1
    i'll upvote this for Suhafer, but Ekko Sky, i'll suggest you also give this a try with Gson, for parsing. The library is great, and will take you a short time to learn & use. As your response grows, it becomes difficult to cater it with default JSONObject or JSONArray to use, Gson is in my opinion the best way to do it. Happy coding! cheers – vibhor_shri Nov 03 '17 at 08:40
0

You can do this as below by using jettinson.jar

 try {
        String data = "{\"server_response\":[{\"violation\":\"Driving with No Helmet\"},{\"violation\":\"Try\"}]}";
        JSONObject jsonObject = new JSONObject(data);
        JSONArray temp = jsonObject.getJSONArray("server_response");
        int length = temp.length();
        if (length > 0) {
            String[] recipients = new String[length];
            for (int i = 0; i < length; i++) {
                JSONObject nObject = new JSONObject(temp.getString(i));
                recipients[i] = nObject.getString("violation");
            }
        }
    } catch (JSONException ex) {
        Logger.getLogger(JavaApplication2.class.getName()).log(Level.SEVERE, null, ex);
    }
user2271324
  • 45
  • 2
  • 6
0

Reading your comment I think you want this list to populate on an AutoCompleteTextView. I have written thoroughly what to do this will surely help If you follow these steps carefully. First get the response and convert it to List<String> list = new ArrayList<>() format, Create a ArrayAdapter of this list by

yourListAdapter = new ArrayAdapter<String>(YourActivity.this, 
android.R.layout.simple_list_item_1, list);

After this set your yourAutoCompleteTextBoxName.setAdapter(yourListAdapter); In your Activity initialize this:

yourAutoCompleteTextBoxName.setOnEditorActionList(...){...}

also if you want your list to be clicked from AutoComplete TextView then do this:

yourAutoCompleteTextBoxName.setOnItemClickListener(...){...}
Jason
  • 122
  • 2
  • 11
0

For getting your list of strings:

You can use Jackson's ObjectMapper class.

public class Data {

String violation;
...
}

List<Data> violations = objectMapper.readValue(json, new TypeReference<List<Data>>(){});
Yash
  • 5,225
  • 4
  • 32
  • 65
0

Below: "array" will have all the violations. (You will require some try / catch to surround with). Have a good day!

JSONObject json = new JSONObject(json_string2);

JSONArray violations = json.getJSONArray("server_response");

String[] array = new String[violations.length()];

for(int i = 0; i < violations.length(); i++) {
    JSONObject violation = new JSONObject(violations.getString(i));
    array[i] = violation.getString("violation");
}
Biswas Khayargoli
  • 976
  • 1
  • 11
  • 29
0

You can use Gson library https://github.com/google/gson

Generate Plain Old Java Objects from JSON or JSON-Schema http://www.jsonschema2pojo.org/

YourJsonData jsonObject;
String json_string2 = getIntent().getExtras().getString("json_data");
Gson gson = new Gson();
jsonObject = gson.fromJson(json_string2, YourJsonData.class);

from jsonObject you can get your list i.e server_response array list.

harsh bangari
  • 417
  • 6
  • 19