如何从JSON对象的数据?(how to get data from Json object?)

2019-08-21 04:29发布

我工作的一个Android应用程序,其与Facebook集成。 我使用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对象有所有信息我想要的。 像这样

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

问题是如何存储这些信息分成不同的阵列?

请右键为此,一些代码?

Answer 1:

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

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



Answer 2:

编辑你的代码是这样的...这可能会帮助你..

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


Answer 3:

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

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


文章来源: how to get data from Json object?