-3

抱歉,现在才看到您的答案,我一直在做一些代码:这是我的代码:

ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();


        JSONObject mainJson = new JSONObject(reply);



            JSONArray jsonArray = mainJson.getJSONArray("Companies");



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

                HashMap<String, String> map = new HashMap<String, String>();

                 JSONObject objJson = jsonArray.getJSONObject(i);

                 map.put("ID", objJson.getString("CompanyID"));
                 map.put("name", objJson.getString("CompanyName"));
                 mylist.add(map);



                 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item);
                 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                 spinner.setAdapter(adapter);

但日志显示错误解析数据或.json.exception!!

4

1 回答 1

0

你可以使用:

    class result {
       item[] items;
    }
    class item {
       int id;
       String name;
    }
    private class itemSerializer implements JsonSerializer<item> {
    @Override
            public User deserialize(JsonElement json, Type type,
    JsonDeserializationContext context) throws JsonParseException {
            JsonObject jobject = (JsonObject) json;
            item it = new item();
            it.id = jobject.get("id").getAsInt(), 
            it.name =  jobject.get("name").getAsString()); 
            return it;
           }
    }

com.google.gson.GsonBuilder gson = new GsonBuilder();

    // and to serialize only id and name use custo deserializer:

    gson.registerTypeAdapter(item.class, new itemSerializer());
    result res = new result();
    res = gson.create().fromJson(yourString, res.getClass());

那么你必须加载一个 List<String> names 作为 Splitter 的 ArrayAdapter,可能还有一个 HashMap<String,String> mapNames2Ids 来映射 Name clicked on id

于 2013-04-30T12:20:50.133 回答