我在字符串中得到JSON Response,我分裂并尝试在android微调器中显示,但它在spinner中显示整个字符串

时间:2014-08-28 06:28:12

标签: java android json

String res = getIntent().getStringExtra("Response");
        Log.d("Wallet_Management Response", "getIntent"  +res);
        //Subscriber_Name.setText(res);
        System.out.println("subscriber name response : " + res.toString());



        String [] strings = new String [] {res };
        List<String> stringList = new ArrayList<String>(Arrays.asList(strings));
        //System.out.println("LISTTTTTTTTT : " + stringList);

             for (String s :stringList){
                 String[] str = s.split("\\[");
                 for(String item : str){
                     //System.out.println("Checking"+item);


                     String [] str1 = item.split("\\]");
                     for (String item1 :str1)
                         System.out.println("Whole String"+item1);
                      for(int i =0;i<=str1.length;i++){
                          System.out.println("OUPUT" +str1[0]);
                          //System.out.println("OUPUT" +str1[1]);
                          //System.out.println("OUPUT" +str1[2]);
                      }
                 }
             }






        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, stringList);
                dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                dataAdapter.notifyDataSetChanged();
                spinnerEmp.setAdapter(dataAdapter);

输出是:

08-28 02:05:04.117: D/Wallet_Management Response(1501): getIntent["MaheshKhatmode","Pranav","RameshSippy"]

08-28 02:05:04.117: I/System.out(1501): subscriber name response : ["MaheshKhatmode","Pranav","RameshSippy"]

按如下方式拆分结果后:

08-28 02:05:04.137: I/System.out(1501): Whole String"MaheshKhatmode","Pranav","RameshSippy"

2 个答案:

答案 0 :(得分:1)

这应该有效:

List<String> myStringList = new ArrayList<String>();

try {
    JSONArray array = new JSONArray(res.toString());

    for (int i = 0; i < array.length(); i++) {
        myStringList.add(array.getString(i));
    }
} catch (JSONException e) {
    e.printStackTrace();
}

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, myStringList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
spinnerEmp.setAdapter(dataAdapter);

答案 1 :(得分:0)

你能尝试这样:

Android convert String to String[]

 JSONArray temp = new JSONArray(res);
   String[] str1= temp.join(",").split(",");
                    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    dataAdapter.notifyDataSetChanged();
                    spinnerEmp.setAdapter(dataAdapter);