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! :)

6
  • It is in list form only Commented Nov 3, 2017 at 7:19
  • So would it be possible to get the values Driving with no helmet and Try and attach it to autocomplete textview? Commented Nov 3, 2017 at 7:20
  • stackoverflow.com/a/18998203/6225257 This post explains how to parse json in Java. Commented Nov 3, 2017 at 7:24
  • you need help with the parsing, or you need help with the autocomplete textview, or both? Commented Nov 3, 2017 at 7:32
  • What is your exact requirement Commented Nov 3, 2017 at 7:34

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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! :)
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
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(...){...}

Comments

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);
    }

Comments

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>>(){});

Comments

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");
}

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.