如何存储来自 API 调用的 JSON 数据

发布于 2024-11-06 04:18:42 字数 1723 浏览 0 评论 0原文

我是一名新手程序员,所以请随意评论那些没有意义的事情,以便我可以学习。

我正在开发一个咖啡店查找器 Android 应用程序,该应用程序调用 SimpleGeo API 来获取每个地点的兴趣。我无法想出一种方法来存储返回的 JSON 数据,以便我可以将其用于我的目的。我需要对数据做两件事:首先,我需要一个地图覆盖类来访问它,以便我可以为每个咖啡店绘制标记(我对此表示同意);其次,我需要在列表视图中显示数据。

API 调用:

    /** Queries SimpleGeo with current location **/
    private void getJSONData() {
    SimpleGeoPlacesClient client = SimpleGeoPlacesClient.getInstance();
    client.getHttpClient().setToken(oauth_key, oauth_secret);
    double radius = 25;
    try {
        client.search(lat, lng, "coffee", null, radius,
                new FeatureCollectionCallback() {
                    public void onSuccess(FeatureCollection collection) {
                        try {
                            features = collection.toJSON();
                        } catch (JSONException e) {
                            System.out.println(e.getMessage());
                        }
                    }

                    public void onError(String errorMessage) {
                        System.out.println(errorMessage);
                    }
                });
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
}

解析数据:

    private void parseJSONData() throws JSONException {
    for (int i = 0; i < features.length(); i++) {
        JSONObject feature = features.getJSONObject(i).getJSONObject(
                "feature");
    }
}

我目前正在尝试使用 这个教程。 SQLite 适合这个吗?任何专业提示将不胜感激。

谢谢。

I'm a novice programmer so feel free to comment on things that make no sense so that I can learn.

I'm working on a coffee shop finder android app that makes calls to the SimpleGeo API to get each place of interest. I'm having trouble thinking up a way to store the returned JSON data in a way that I can use for my purposes. I need to do 2 things with the data: first, I need a map overlay class to access it so I can draw markers for each coffee shop (which I'm okay with); second, I need to display the data in a list view.

The API call:

    /** Queries SimpleGeo with current location **/
    private void getJSONData() {
    SimpleGeoPlacesClient client = SimpleGeoPlacesClient.getInstance();
    client.getHttpClient().setToken(oauth_key, oauth_secret);
    double radius = 25;
    try {
        client.search(lat, lng, "coffee", null, radius,
                new FeatureCollectionCallback() {
                    public void onSuccess(FeatureCollection collection) {
                        try {
                            features = collection.toJSON();
                        } catch (JSONException e) {
                            System.out.println(e.getMessage());
                        }
                    }

                    public void onError(String errorMessage) {
                        System.out.println(errorMessage);
                    }
                });
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
}

Parsing the data:

    private void parseJSONData() throws JSONException {
    for (int i = 0; i < features.length(); i++) {
        JSONObject feature = features.getJSONObject(i).getJSONObject(
                "feature");
    }
}

I'm currently trying to create java objects out of the data using GSON outlined in this tutorial. Would SQLite be appropriate for this? Any pro-tips would be greatly appreciated.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱桃奶球 2024-11-13 04:18:42

假设您正在使用 android,您可以使用 google 地图视图

,com.google.android.maps.MapView
http://developer.android.com/resources/tutorials/views/hello -mapview.html

和 ListView
http://developer.android.com/reference/android/widget/ListView.html

我不明白 SQLite 如何适应这里,除非您计划在应用程序重新启动时存储这些位置。
您所需要做的就是从 JSON 中提取地点信息,然后对这些数据执行您想要的任何操作。

Assuming you are working with android, you can use google map view

view,com.google.android.maps.MapView
http://developer.android.com/resources/tutorials/views/hello-mapview.html

and ListView
http://developer.android.com/reference/android/widget/ListView.html

I dont see how SQLite fits in here , unless you plan to store the places across app restarts.
All you need to do is extract place information from JSON and do whatever you want with that data.

时光病人 2024-11-13 04:18:42

我意识到答案已经被接受,但无论如何我都将我的 2 位扔进去...

可能不需要持久化响应 JSON(在 SQLite 或文本文件中),以便“避免不必要的 API”打电话。”需要有关该应用程序的更多信息才能确定这一点。

摘自朋友让我校对的博客文章:

在某些情况下,GSON 使得从 JSON 创建 Java 对象变得异常简单,尽管许多示例(尤其是博客文章中的示例)都非常琐碎或不必要的复杂,通常很小不仅仅是从 GSON 文档中复制粘贴示例(名称已更改),这是问题的核心 - 文档的示例并不十分详尽,并且它们很快就会使复杂的对象看起来很难映射。

但我离题了……

如果问题是人们不知道如何使用 JSON 和/或 GSON,这并不意味着该解决方案涉及 SQLite 或文本文件持久性或任何类型的持久性。

关于原始问题中的代码,看起来它假设响应 JSON 中的对象将以某种特定顺序出现。根据我的经验,这通常不是一个安全的假设。有时 itemX 排在第一位,有时排在第三位。 (这当然取决于生成响应的系统。)因此,解析结果时允许 JSON 对象的顺序发生变化。

关于是否保留 JSON 数据,与到目前为止所解释的内容相比,需要更多地了解该应用程序。

I realize that an answer was already accepted, but I'm tossing my 2 bits in, anyhow...

It may not be that the response JSON needs be persisted (in SQLite or a text file), in order to "avoid unnecessary API calls." More information about the app would be needed to determine this.

Copying from a blog post my friend asked me to proofread:

In some situations, GSON makes it fantastically simple to create Java objects from JSON, though many of the examples (especially those in blog posts) are lamely trivial or unnecessarily complex, often being little more than copy-paste examples (with names changed) from the GSON documentation, which is the heart of the problem - the docs aren't very thorough in their examples, and they quickly make it seem like complex objects are complicated to map.

But I digress...

If the problem is that one doesn't know how to use JSON and/or GSON, this does not imply that the solution involves SQLite or text file persistence or any kind of persistence.

Regarding the code in the original question, it looks like it assumes that objects in the response JSON will appear in some particular order. It's my experience that this is often not a safe assumption to make. Sometimes itemX comes first, sometimes it comes third. (This of course depends on the system generating the response.) So, parse the result allowing for the order of JSON objects to vary.

Regarding whether to persist the JSON data, again, more would need to be known about the app than what was explained, so far.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文