sephtian
sephtian

Reputation: 455

parse json array to normal array in android

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

Upvotes: 0

Views: 667

Answers (2)

Jigar Pandya
Jigar Pandya

Reputation: 5987

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,

Upvotes: 0

Programmer
Programmer

Reputation: 5400

            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

Upvotes: 2

Related Questions