类型不匹配:无法从Object转换为JSONObject

时间:2013-03-13 05:15:44

标签: android json

我对Android开发相对较新,正在编写我的第一个基于REST的应用程序。我选择使用Android Asynchronous HTTP Client让事情变得更容易一些。我目前只是在该链接上运行主要的“推荐用法”部分,基本上只是创建一个基本的静态HTTP客户端。我正在遵循给定的代码,但改变它以引用不同的API。这是有问题的代码:

    public void getFactualResults() throws JSONException {
    FactualRestClient.get("q=Coffee,Los Angeles", null, new JsonHttpResponseHandler() {
        @Override
        public void onSuccess(JSONArray venues) {
            // Pull out the first restaurant from the returned search results
            JSONObject firstVenue = venues.get(0);
            String venueName = firstVenue.getString("name");

            // Do something with the response
            System.out.println(venueName);

        }
    });
}

String venueName = firstVenue.getString("name");行当前在Eclipse中引发错误:“类型不匹配:无法从Object转换为JSONObject”。为什么会出现此错误?我搜索了其他线程,这导致我尝试使用getJSONObject(0)而不是get(0),但这导致了进一步的错误,Eclipse建议使用try / catch。除了变量名和URL之外,我没有更改教程中的任何代码。有什么想法/提示/建议吗?

非常感谢。

编辑:

这是onSuccess方法,修改为包含建议的try / catch块。 Eclipse现在在这里显示firstVenue的“本地变量可能尚未初始化”:venueName = firstVenue.getString("name");和在此处的venueName:System.out.println(venueName);即使我在String venueName;之后直接初始化JSONObject firstVenue;我仍然得到同样的错误。任何帮助解决这些问题都将非常感谢!

public void onSuccess(JSONArray venues) {
            // Pull out the first restaurant from the returned search results
            JSONObject firstVenue;
            try {
                firstVenue = venues.getJSONObject(0);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String venueName;
            try {
                venueName = firstVenue.getString("name");
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // Do something with the response
            System.out.println(venueName);

        }

4 个答案:

答案 0 :(得分:1)

您可以尝试将从查询到的对象转换为String,然后使用

final JSONObject jsonObject = new JSONObject(stringresult);

我之前遇到同样的错误,它对我有用。

答案 1 :(得分:0)

是的,您应该使用getJSONObject来确保您获得的值是JSON对象。是的,如果数组中的索引不存在或者不包含对象,则应该捕获可能引发的JSONException

它看起来像这样:

JSONObject firstVenue;
try {
    firstVenue = venues.get(0);
} catch (JSONException e) {
    // error handling
}

答案 2 :(得分:0)

将obj转换为json对象:

Object obj = JSONValue.parse(inputParam);  JSONObject jsonObject =(JSONObject)obj;

答案 3 :(得分:0)

Shail Adi提供的解决方案仅通过将firstVenue和venueName的初始值设置为null来为我工作。这是我的代码:

        JSONObject firstVenue = null;
        try {
            firstVenue = (JSONObject)venues.get(0);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String venueName = null;
        try {
            venueName = firstVenue.getString("name");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // Do something with the response
        System.out.println(venueName);
相关问题