如何从Json对象获取数据?

时间:2022-12-04 16:01:01

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

我正在开发一个与Facebook集成的Android应用程序。我正在使用fql查询从Facebook获取信息。我的fql方法是

                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

myjson对象拥有我想要的所有信息。像这样

 {"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.?

请为此目的找一些代码。?

3 个解决方案

#1


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/

#2


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")+": ");

#3


0  

In java, It's solved as

在java中,它解决了

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

在javascript中,它已经解决了

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

#1


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/

#2


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")+": ");

#3


0  

In java, It's solved as

在java中,它解决了

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

在javascript中,它已经解决了

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