如何在Android Studio中获取没有JSONArray关键字的JSONArray

时间:2015-12-10 11:15:23

标签: android-studio android-spinner

我是这个JSON的新手,我正在尝试获取JSON值,它实际上是没有名称的JSONArray。我已经浏览了一些他们通过JSONArray Keyword访问的教程。但是在我的JSON文件中没有这样的ArrayName。请帮我解决这个问题。这是我的JSON值:

 [
   {"id":"4","name":"Trichy"},
   {"id":"5","name":"Pondy"},
   {"id":"6","name":"Kovai"},
   {"id":"7","name":"Madurai"},
    {"id":"8","name":"Chennai"},
   {"id":"9","name":"Hyderabad"}
   ]

我需要从中得到“名字”。这是我的MainActivity文件。

        try {
            // Locate the NodeList name
            JSONArray json=new JSONArray(???); //I dont know what to give here 
            for (int i = 0; i < json.length(); i++) {
                jsonobject = jsonarray.getJSONObject(i);

                Cites worldpop = new Cites();
                worldpop.setCity(jsonobject.optString("name"));

                cit.add(worldpop);

                // Populate spinner with country names
                worldlist.add(jsonobject.optString("name"));

            }
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }

这是我的City.java文件。

package com.example.user.spinnercontrol;


public class Cites {
private String city;

public String getCity()
{
    return city;
}
public void setCity(String city)
{
    this.city=city;
}
}

这是我的JSON.java文件(供参考)

package com.example.user.spinnercontrol;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
    import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONCity {

public static JSONObject getJSONfromURL(String url) {
    InputStream is = null;
    String result = "";
    JSONObject jArray = null;

    // Download JSON data from URL
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();

    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection " + e.toString());
    }

    // Convert response to string
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();
    } catch (Exception e) {
        Log.e("log_tag", "Error converting result " + e.toString());
    }

    try {

        jArray = new JSONObject(result);
    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());
    }

    return jArray;
}
}

1 个答案:

答案 0 :(得分:0)

MainActivity类应该是这样的::

public class MainActivity extends Activity {
JSONObject jsonobject;
ArrayList<String> worldlist = new ArrayList<>();
ArrayList<Cites> cit = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    JSONArray arr = JSONCity.getJSONfromURL("http://www.prateektechnosoft.com/justcall/cities/getall");

    for (int i = 0; i < arr.length(); i++) {

        try {
            jsonobject = arr.getJSONObject(i);

            Cites worldpop = new Cites();
            worldpop.setCity(jsonobject.optString("name"));

            cit.add(worldpop);

            // Populate spinner with country names
            worldlist.add(jsonobject.optString("name"));

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    Spinner mySpinner = (Spinner) findViewById(R.id.my_spinner);

    // Spinner adapter
    mySpinner
            .setAdapter(new ArrayAdapter<String>(MainActivity.this,
                    android.R.layout.simple_spinner_dropdown_item,
                    worldlist));

    // Spinner on item click listener
    mySpinner
            .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> arg0,
                                           View arg1, int position, long arg3) {

                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                    // TODO Auto-generated method stub
                }
            });


}

}