0

i have JSON data from php -it's one dimensional array-:

{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"]}

and in android i want to save that JSON array to an array so i would like to get array like this

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"]

im trying this

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));

but still didnt work

2
  • what is not working arnt you what data are you getting in array Commented Jul 7, 2012 at 7:46
  • @Programmer, i just edit my question, the code=> teks1.setText(Arrays.toString(pairs)); did not show the array Commented Jul 7, 2012 at 7:49

2 Answers 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
2
  • String i[] <== eclipse want this in "Object" data type Commented Jul 7, 2012 at 7:55
  • but i want that in String and NOT object, i do looping and use .toString() but still not working Commented Jul 7, 2012 at 8:04
0

Try following code block..

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());
   } 
    } 
 }

Thanks,

2
  • i want to get that in array, not arraylist, cuz i cant convert from array list into array Commented Jul 7, 2012 at 7:45
  • it is in array i guess... Array<String> list = new Array<String>(); Commented Jul 7, 2012 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.