0

我有来自 php 的 JSON 数据-它是一维数组-:

{pair:["-8.5745000,115.3735700","-8.5683300,115.3733700","-8.5683300,115.3733700","-8.5687800,115.3997700","-8.5687800,115.3997700","-8.5764900,115.4007400","-8.5764900,115.4007400","-8.5687800,115.3997700","-8.5687800,115.3997700","-8.5656400,115.4156400","-8.5656400,115.4156400","-8.5565200,115.4122800","-8.5565200,115.4122800","-8.5566200,115.4110500","-8.5566200,115.4110500","-8.5560700,115.4112200","-8.5560700,115.4112200","-8.5554200,115.4112800","-8.5554200,115.4112800","-8.5527200,115.4025400","-8.5527200,115.4025400","-8.5424000,115.4027000","-8.5424000,115.4027000","-8.5426600,115.4055800","-8.5426600,115.4055800","-8.5377000,115.4057200","-8.5377000,115.4057200","-8.5375900,115.4034500","-8.5375900,115.4034500","-8.5358900,115.4036500","-8.5358900,115.4036500","-8.5358800,115.4033400"]}

在android中我想将该JSON数组保存到一个数组中,所以我想得到这样的数组

pair=["-8.5745000,115.3735700","-8.5683300,115.3733700","-8.5683300,115.3733700","-8.5687800,115.3997700","-8.5687800,115.3997700","-8.5764900,115.4007400","-8.5764900,115.4007400","-8.5687800,115.3997700","-8.5687800,115.3997700","-8.5656400,115.4156400","-8.5656400,115.4156400","-8.5565200,115.4122800","-8.5565200,115.4122800","-8.5566200,115.4110500","-8.5566200,115.4110500","-8.5560700,115.4112200","-8.5560700,115.4112200","-8.5554200,115.4112800","-8.5554200,115.4112800","-8.5527200,115.4025400","-8.5527200,115.4025400","-8.5424000,115.4027000","-8.5424000,115.4027000","-8.5426600,115.4055800","-8.5426600,115.4055800","-8.5377000,115.4057200","-8.5377000,115.4057200","-8.5375900,115.4034500","-8.5375900,115.4034500","-8.5358900,115.4036500","-8.5358900,115.4036500","-8.5358800,115.4033400"]

我正在尝试这个

String []pairs=null;
        String hasil = "";
        hasil = getRequest(url);
        try 
        {
            jObject = new JSONObject(hasil);
            JSONArray myArray = jObject.getJSONArray("pair");
            for(int i = 0; i < myArray.length(); i++)
            {
                pairs[i]= myArray.get(i).toString();
            }


        } 
        catch (JSONException e)
        {
            e.printStackTrace();
        }
        teks1 = (TextView)findViewById(R.id.textView1);
        teks1.setText(Arrays.toString(pairs));

但仍然没有工作

4

2 回答 2

2
            ArrayList<String> list = new ArrayList<String>();     
            JSONArray jsonArray = (JSONArray)jsonObject; 
            if (jsonArray != null) { 
               int len = jsonArray.length();
               for (int i=0;i<len;i++){ 
                list.add(jsonArray.get(i).toString());
               } 
                } 
             }
           String i[] = list.toArray();   //get array
于 2012-07-07T07:49:36.420 回答
0

尝试以下代码块..

Array<String> list = new Array<String>();     
JSONArray jsonArray = (JSONArray)jsonObject; 
if (jsonArray != null) { 
   int len = jsonArray.length();
   for (int i=0;i<len;i++){ 
    list.add(jsonArray.get(i).toString());
   } 
    } 
 }

谢谢,

于 2012-07-07T07:41:27.880 回答