Java 将JSON解析为对象并在分层微调器中显示它?

Java 将JSON解析为对象并在分层微调器中显示它?,java,android,json,Java,Android,Json,我正在编写一个从web主机访问JSON文件的应用程序。这些文件包含关于不同位置、设施等的信息。我试图提出一种方法,使我们能够快速轻松地解析信息,并将其显示到分层微调器中,以微调信息 在网站上,我们使用树状视图,所以这是我的移动解决方案 我真的不知道有什么好的、有效的方法来解析所有内容,而且我现在所做的似乎有点混乱,下面是我目前所做的一个示例: private void generatePlants(String json) { try { JSONObject jObj

我正在编写一个从web主机访问JSON文件的应用程序。这些文件包含关于不同位置、设施等的信息。我试图提出一种方法,使我们能够快速轻松地解析信息,并将其显示到分层微调器中,以微调信息

在网站上,我们使用树状视图,所以这是我的移动解决方案

我真的不知道有什么好的、有效的方法来解析所有内容,而且我现在所做的似乎有点混乱,下面是我目前所做的一个示例:

private void generatePlants(String json) {
    try {
        JSONObject jObject = new JSONObject(json);
        String aJsonString = jObject.getString("company");
        jObject = new JSONObject(aJsonString);
        JSONArray jArray = jObject.getJSONArray("plant");

        plants = new String[jArray.length()+1];
        plants[0] = "";

        for (int i = 0; i < jArray.length(); i++) {
                JSONObject unitObject = jArray.getJSONObject(i);
                plants[i+1] = unitObject.getString("plantLocation");
        }

        Spinner spinner = (Spinner)getActivity().findViewById(R.id.spnLocation);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_spinner_item, plants);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
    } catch (Exception e) {
        Log.e("SolidCM", "exception", e);
    }
}

private void generateUnits(String json, int plant) {
    try {
        JSONObject jObject = new JSONObject(json);
        String aJsonString = jObject.getString("company");
        jObject = new JSONObject(aJsonString);
        JSONArray jArray = jObject.getJSONArray("plant");
        aJsonString = jArray.getString(plant);
        Log.d("DEBUG",aJsonString);
        jObject = new JSONObject(aJsonString);
        jArray = jObject.getJSONArray("unit");

        units = new String[jArray.length()+1];
        units[0] = "";

        for (int i = 0; i < jArray.length(); i++) {
            JSONObject unitObject = jArray.getJSONObject(i);
            units[i+1] = unitObject.getString("unitName");
        }

        Spinner spinner = (Spinner)getActivity().findViewById(R.id.spnView);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_spinner_item, units);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
    } catch (Exception e) {
        Log.e("SolidCM", "exception", e);
    }
}

private String getJson() {
    DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
    HttpPost httppost = new HttpPost("http://jdhpro.com/downloads/testData.json");

    httppost.setHeader("Content-type", "application/json");

    InputStream inputStream = null;
    String result = null;
    try {
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();

        inputStream = entity.getContent();
        // json is UTF-8 by default
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();

        String line = null;
        while ((line = reader.readLine()) != null)
        {
            sb.append(line + "\n");
        }
        result = sb.toString();
    } catch (Exception e) {
        Log.e("SolidCM", "exception", e);
    }
    finally {
        try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
        return result;
    }
}
private void generatePlants(字符串json){
试一试{
JSONObject jObject=新的JSONObject(json);
字符串aJsonString=jObject.getString(“公司”);
jObject=新的JSONObject(aJsonString);
JSONArray jArray=jObject.getJSONArray(“工厂”);
plants=新字符串[jArray.length()+1];
植物[0]=“”;
for(int i=0;i
我个人的方法是将每个JSON对象表示为Java类。一种流行的方法是使用,尽管我从未使用过,也不能就此提供进一步的建议

不过,你可以自己做。从顶部开始使用“company”JSON对象,该对象将由以下Java类表示

public class Company {
    private String mCompanyId;
    private String mCompanyName;

    // Fields for address, phone, email etc

    private ArrayList<Plant> mPlantList;

    // Getters and setters here

    public Company (String jsonString) {
        // The jsonString will be the value (object) of the
        // "company" : {...} JSON name/value pair.
        // Parse the JSON for companyId, companyName, address etc
        // until you get to the "plant" : [...]" JSON array. Iterate
        // over the array creating a new Plant Java object passing in
        // the JSON object for each plant
    }
}
public class Company {
    ...
    public String toString() {
        return mCompanyName;
    }
}
带有
ArrayList
的示例(假设您有多家公司)

ArrayList companyList=新的ArrayList;
//解析JSON并将公司添加到ArrayList
ArrayAdapter=新的ArrayAdapter(getActivity(),
android.R.layout.simple\u微调器\u项目,公司列表);
public class Company {
    ...
    public String toString() {
        return mCompanyName;
    }
}
ArrayList<Company> companyList = new ArrayList<Company>;

// Parse your JSON and add the companies to the ArrayList

ArrayAdapter<Company> adapter = new ArrayAdapter<Company>(getActivity(),
            android.R.layout.simple_spinner_item, companyList);