简体   繁体   English

如何通过API调用存储JSON数据

[英]How to store JSON data from API call

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. 我正在开发一个咖啡店搜索器android应用程序,该应用程序调用SimpleGeo API以获取每个感兴趣的地方。 I'm having trouble thinking up a way to store the returned JSON data in a way that I can use for my purposes. 我在想一种可以用于我的目的的方式来存储返回的JSON数据时遇到了麻烦。 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: 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());
    }
}

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. 我目前正在尝试使用教程中概述的GSON从数据中创建Java对象。 Would SQLite be appropriate for this? SQLite是否适合于此? Any pro-tips would be greatly appreciated. 任何亲提示将不胜感激。

Thanks. 谢谢。

Assuming you are working with android, you can use google map view 假设您使用的是Android,则可以使用Google Map View

view,com.google.android.maps.MapView http://developer.android.com/resources/tutorials/views/hello-mapview.html 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 和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. 我看不到SQLite如何适合这里,除非您计划在应用程序重新启动时存储这些位置。 All you need to do is extract place information from JSON and do whatever you want with that data. 您需要做的就是从JSON提取位置信息,并对数据进行任何操作。

I realize that an answer was already accepted, but I'm tossing my 2 bits in, anyhow... 我意识到答案已经被接受,但是无论如何,我都在扔我的2位...

It may not be that the response JSON needs be persisted (in SQLite or a text file), in order to "avoid unnecessary API calls." 为了“避免不必要的API调用”,响应JSON可能不需要持久化(在SQLite或文本文件中)。 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. 在某些情况下,GSON使从JSON创建Java对象变得异常简单,尽管许多示例(尤其是博客文章中的示例)都琐碎琐碎或不必要地复杂,通常只不过是复制粘贴示例(名称更改)而已。 GSON文档,这是问题的核心-文档在示例中并不十分详尽,并且很快使复杂的对象看起来很难映射。

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. 如果问题是一个人不知道如何使用JSON和/或GSON,则并不意味着该解决方案涉及SQLite或文本文件的持久性或任何类型的持久性。

Regarding the code in the original question, it looks like it assumes that objects in the response JSON will appear in some particular order. 关于原始问题中的代码,它似乎假定响应JSON中的对象将以某种特定顺序出现。 It's my experience that this is often not a safe assumption to make. 根据我的经验,这通常不是一个安全的假设。 Sometimes itemX comes first, sometimes it comes third. 有时itemX在第一位,有时在第三位。 (This of course depends on the system generating the response.) So, parse the result allowing for the order of JSON objects to vary. (当然,这取决于生成响应的系统。)因此,解析结果以允许JSON对象的顺序变化。

Regarding whether to persist the JSON data, again, more would need to be known about the app than what was explained, so far. 关于是否持久化JSON数据,再次需要了解的应用程序要比到目前为止解释的更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM