0

Hi in my android application I'm receiving a JSON response from server and it has no key value for it,Now I should parse that JSON and that parsed data should be appended to a spinner. How to achieve that ? This is what I have tried

This is my JSON 
 [["Basic","Premium","svdsv","uymyimy"]] 
String url = "http://www.thetaf.com/TAFCycleStation/get_plans.php";
 try {
 JSONArray data = new JSONArray(getJSONUrl(url));
 List<String> list = new ArrayList<String>();
 for(int i = 0; i < data.length(); i++) 
 {
 JSONObject c = data.getJSONObject(i);
 list.add(c.getString("0"));
 }
 ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
 dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 plans.setAdapter(dataAdapter);
 } catch (JSONException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 }
 public String getJSONUrl(String url) {
 StringBuilder str = new StringBuilder();
 HttpClient client = new DefaultHttpClient();
 HttpGet httpGet = new HttpGet(url);
 try {
 HttpResponse response = client.execute(httpGet);
 StatusLine statusLine = response.getStatusLine();
 int statusCode = statusLine.getStatusCode();
 if (statusCode == 200) { // Download OK
 HttpEntity entity = response.getEntity();
 InputStream content = entity.getContent();
 BufferedReader reader = new BufferedReader(new InputStreamReader(content));
 String line;
 while ((line = reader.readLine()) != null) {
 str.append(line);
 }
 } else {
 Log.e("Log", "Failed to download result..");
 }
 } catch (ClientProtocolException e) {
 e.printStackTrace();
 } catch (IOException e) {
 e.printStackTrace();
 }
 return str.toString();
 }
asked May 27, 2015 at 6:23
7
  • 1
    How can it be JSON if it has no key value? Commented May 27, 2015 at 6:25
  • can you show the json once Commented May 27, 2015 at 6:25
  • Yes show us the json Commented May 27, 2015 at 6:26
  • @RanjitPati [["Basic","Premium","svdsv","uymyimy"]] this is my JSON Commented May 27, 2015 at 6:26
  • 1
    AFAIK its not a JSON.. Commented May 27, 2015 at 6:28

2 Answers 2

3

Try like the following:

try {
 JSONArray jsonArray1=new JSONArray(jsonString);
 JSONArray jsonArray2=jsonArray1.getJSONArray(0);
 for (int i = 0; i < jsonArray2.length(); i++) {
 String value=jsonArray2.getString(i);
 System.out.println(value);
 }
} catch (JSONException e) {
 e.printStackTrace();
}

Add the value to your list as required.

answered May 27, 2015 at 6:30
Sign up to request clarification or add additional context in comments.

Comments

0

Try this.

 JSONArray obj = null;
 try {
 obj = new JSONArray(YOURJSON);
 } catch (JSONException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 for (int i=0; i<obj.length(); i++){
 try {
 String extString = (String) obj.get(i);
 System.out.println("Your string");
 } catch (JSONException e) {
 e.printStackTrace();
 }
 }

My JSON :

[ "a", "about", "above", "after", "again", "against", "all", "am", "an", "and", "any", "are", "aren't", "as", "at", "be", "because", "been", "before", "being", "below", "between", "both", "but", "by", "can't", "cannot"]

answered May 27, 2015 at 6:31

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.