1

我知道这可能已经被问到了,但我无法得到具体的答案。我有一个 json 字符串:

GraphObject{graphObjectClass=GraphPlace, state={"id":"268367713308665","category":"School","location":{"state":"","zip":"","longitude":32.631482614136,"latitude":0.24519375867929,"country":"Uganda","city":"Kampala","street":"PO BOX 28493"},"category_list":[{"id":"365182493518892","name":"School"}],"name":"Little Wonders Montessori"}}
GraphObject{graphObjectClass=GraphPlace, state={"id":"142442605900768","category":"Education","location":{"state":"","zip":"","longitude":32.606752647018,"latitude":0.28746491511647,"country":"Uganda","city":"Kampala","street":"Heritage\/ Kiwafu Stage, Wheeling Zone, Gaba Road, Kansanga"},"category_list":[{"id":"151676848220295","name":"Education"}],"name":"Wisdomgate Pre-School, Kansanga."}}

当我查询附近的地方时,我从 facebook 获得,我想提取纬度、经度、类别和名称。这就是我正在尝试的

public void onCompleted(List<GraphPlace> places, Response response) {
    //Toast.makeText(getActivity(), (CharSequence) places.listIterator(), Toast.LENGTH_LONG).show();
    ListIterator<GraphPlace> x =    places.listIterator();
    while(x.hasNext()){
        String f = x.next().toString();
        Toast.makeText(getActivity(), f, Toast.LENGTH_LONG).show();
        mytext.setText(f);
        try {
            JSONObject json = new JSONObject(f);
            JSONArray array = json.getJSONArray( "GraphPlace" );
            Toast.makeText(getActivity(), array.length(), Toast.LENGTH_LONG).show();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}   
});
request.executeAsync(); 

但是,我似乎无法获得入口点,无论我尝试多少,我都无法得到任何东西提前感谢您的帮助。

4

1 回答 1

1

如果您的 JSON 字符串如下所示:

{"id":"268367713308665","category":"School","location":{"state":"","zip":"","longitude":32.631482614136,"latitude":0.24519375867929,"country":"Uganda","city":"Kampala","street":"PO BOX 28493"},"category_list":[{"id":"365182493518892","name":"School"}],"name":"Little Wonders Montessori"}}

要获得纬度和经度将是:

        JSONObject json = new JSONObject(yourJSONVariableHere);
        JSONObject location = json.getJSONObject( "Location" );
        String lat = location.getString("latitude");
        String long = location.getString("longitude");

对于latlong变量,您可以分配intlong取决于 JSON 的值。如果你改变变量类型,你会为各自的变量值做getInt()or getLong

要记住的另一件事是你有一个嵌套对象,它包含latand long,所以你必须得到它,然后解析它。如果您有嵌套数组或其他对象,情况也是如此。目前您正在使用getJSONArray, 但 JSON 中的数组有[array, array]或方括号,其中 JSON 对象有{object, object}花括号。

希望这可以帮助。

于 2013-09-24T03:45:30.733 回答