繁体   English   中英

JSON解析海量数据

[英]JSON parsing for huge data

我必须从Web服务下载40,000多个JSON记录,但是下载时间很长。 下载的数据存储在字符串中,但是我无法解析该字符串。 请告诉我解决此问题的解决方案。

网址: https//buzoonga.co.uk/appapi/contacts.php?user_id = 531

public JSONObject getJSONFromUrl(String url) {

    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();           

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}  

查看gzip。 优秀的文本压缩器。 您将要删除〜70%的大小:),只需将其gzip压缩到服务器端,然后将其解压缩到客户端即可。 如果这种开销增加了时间,但下载量只有时间的三分之一,那么您最终可能会获胜。

还可以考虑设计一个更好的架构来减小json的大小。 喜欢,而不是“ animalBodyParts”放“ abp”之类的东西。 您还没有提供json的示例,所以我只是在猜测。

您可以使用JSONTokener类并传递InputStream

jObj = new JSONObject(new JSONTokener(httpEntity.getContent()));

为了减少下载时间,您需要在服务器端支持压缩下载,或将JSON结构更改为更紧凑的基于数组的格式

暂无
暂无

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

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