how to get data from Json object?
Asked Answered
P

3

6

i am working on a android app which is integrated with facebook . i am using fql query to fetch info from facebook. my fql method is

                void runfql(){
                String fqlQuery = "SELECT uid, name, pic_small,birthday FROM user WHERE uid IN " +
                  "(SELECT uid2 FROM friend WHERE uid1 = me() )";
                Bundle params = new Bundle();
                params.putString("q", fqlQuery);
                Session session = Session.getActiveSession();
                Request request = new Request(session,
                "/fql",                         
                params,                         
                HttpMethod.GET,                 
                new Request.Callback(){         
                        public void onCompleted(Response response) {

                        JSONObject myjson=response.getGraphObject().getInnerJSONObject();
                        Log.d("ResultResultResult: " ,""+myjson);                           
                        }                  
                    }); 
                    Request.executeBatchAsync(request);  

                } 

myjson object has all info i want. like this

 {"data":[{"uid":536089174,"birthday":"July 22","name":"Usman Aslam Sheikh"},{"uid":581379174,"birthday":"July 26","name":"Ammar Khalid"}

question is how to store that information into different arrays??

please right some code for this purpose.?

Prepared answered 10/4, 2013 at 6:50 Comment(1)
Did you look for JSONObject and JSONArray? or GSON maybe?Ostrich
K
10
String jsonString = yourstring;
JSONObject jsonResult = new JSONObject(jsonString);
JSONArray data = jsonResult.getJSONArray("data");
if(data != null) {
    String[] names = new String[data.length()];
    String[] birthdays = new String[data.length()];
    for(int i = 0 ; i < data.length() ; i++) {
        birthdays[i] = data.getString("birthday");
        names[i] = data.getString("name");
    }
}

check http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

Keenakeenan answered 10/4, 2013 at 6:53 Comment(2)
what for this kind of object? [{"id":"1019","level":"1","time_in_secs":"3","par":"0","initials":"EGB","quote":"Losers! i am way awesomer","time_stamp":"2014-01-26 11:20:20"},{"id":"1018","level":"1","time_in_secs":"3","par":"0","initials":"EGB","quote":"you just got beat by a girl!","time_stamp":"2014-01-26 11:19:24"},{"id":"1032","level":"1","time_in_secs":"3","par":"0","initials":"0","quote":"Losers!","time_stamp":"2014-07-10 09:11:55"}]Neritic
String jsonString = yourstring; JSONObject jsonResult = new JSONObject(jsonString); JSONArray data = jsonResult.getJSONArray(""); if(data != null) { String[] names = new String[data.length()]; String[] birthdays = new String[data.length()]; for(int i = 0 ; i < data.length() ; i++) { birthdays[i] = data.getString("id"); names[i] = data.getString("level"); } }Etsukoetta
L
0

Edit your code like this ...this may help you..

JSONObject resultObject = new JSONObject(response);

JSONArray JArray = resultObject.getJSONArray("data");

                for (int t=0; t<JArray.length(); t++) {

                    JSONObject JObject = JtArray.getJSONObject(t);



builder.append(JObject.getString("uid")+": ");
Labio answered 10/4, 2013 at 7:0 Comment(0)
P
0

In java, It's solved as

String jsonString = yourstring;
JSONObject data = JSON.parse(jsonString);

//reading data using index
long firstData_uid = data["data"][0]["uid"];
String firstData_birthday = data["data"][0]["birthday"];
String firstData_name = data["data"][0]["name"];

In javascript, It's solved as

var jsonString = yourstring;
var data = JSON.parse(jsonString);

//reading data using index
var firstData_uid = data["data"][0]["uid"];
var firstData_birthday = data["data"][0]["birthday"];
var firstData_name = data["data"][0]["name"];
Psychogenic answered 5/7, 2018 at 3:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.