如何在没有GSON的情况下解析安卓系统中的URL或restful服务的巨大JSON数据?[英] how to parse huge JSON data froma url or restful service in android?without GSON

本文是小编为大家收集整理的关于如何在没有GSON的情况下解析安卓系统中的URL或restful服务的巨大JSON数据?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个休息的服务,......我正在得到一个json数据,...

{
"returnCode": "success",
"RecievedData": {
"results": [
  {
    "details": [
      {
        "moredetails": [{
          "id": "123456",
          "price": "129.99",
          "recorded_at": 3223322,
          "lastrecorded_at": 0002020,
          "seller": "google",
          "availability": "Available",
          "currency": "USD"
        }],
        "offers_count": 1,
        "name": "google.com",
        "recentoffers_count": 1,
        "sid": "988008555566649",
        "url": "http://google.com"
      },
      "moredetails"{
                .
                .

               }
      ] details
         { 
         { 
          [
           ]
          }
    "model": "abc",
    "weight": "127",
    "price_currency": "USD",
    "features": {
      .
      '
      }
     "model": "abc",
    "weight": "127",
    "price_currency": "USD",
    .
    .
    .

我正在使用这个

示例或教程

并且在它从这个 url

那里的一个多碟,他是用这个json对象jsonarray = jsonobject.getJSONArray("worldpopulation");

的数据解析数据

所以我从URL中获取数据,如上所述如何解析这么大的JSON数据,因为我想要的JSON阵列的2或3个字段.

请帮助,..用一个工作的例子,...为上面,..

推荐答案

您可以使用GSON库.有一个例子

其他推荐答案

因为它是嵌套的json,并且您尚未指定您需要哪个字段难以回答 使用此类作为借记类使用方法从您需要显示数据, 它将返回阵列的第一个对象.

公共类解析器{

    public String getData() {
        String content = null;

        String result = "";
        InputStream is = null;

        // http get
        try {
            HttpClient httpclient = new DefaultHttpClient();

            String webUrl = "your url";

            HttpGet httppost = new HttpGet(webUrl);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();

            is = entity.getContent();

        } catch (Exception e) {
            Log.d("LOGTAG", "Error in http connection " + e.toString());
        }

        // convert response to string
        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();
            result = sb.toString();
            Log.d("LOGTAG", "Result :" + result);
        } catch (Exception e) {
            Log.d("LOGTAG", "Error converting result " + e.toString());
        }

        // parse json data

        try {

            JSONArray jarray = new JSONArray(result);
            JSONObject jobj = jarray.getJSONObject(jarray.getInt(0));
            content = jobj.getString("key");//key=name of the field you require
        } catch (Exception e) {

            Log.d("LOGTAG", e.toString());

        }
        return content;
    }
}

其他推荐答案

我看到了你的评论......你不想改变你的代码.但是,与使用GSON的比较解析了一个json自己很慢.使用gson非常简单,你只需要创建简单的pojo类,它会将整个json转换为您可以在任何地方使用的Java对象.这也是一种简洁的方式.这里是一个例子

本文地址:https://www.itbaoku.cn/post/102547.html

问题描述

i have a Rest ful service,.. and i am getting a JSON data like,..

{
"returnCode": "success",
"RecievedData": {
"results": [
  {
    "details": [
      {
        "moredetails": [{
          "id": "123456",
          "price": "129.99",
          "recorded_at": 3223322,
          "lastrecorded_at": 0002020,
          "seller": "google",
          "availability": "Available",
          "currency": "USD"
        }],
        "offers_count": 1,
        "name": "google.com",
        "recentoffers_count": 1,
        "sid": "988008555566649",
        "url": "http://google.com"
      },
      "moredetails"{
                .
                .

               }
      ] details
         { 
         { 
          [
           ]
          }
    "model": "abc",
    "weight": "127",
    "price_currency": "USD",
    "features": {
      .
      '
      }
     "model": "abc",
    "weight": "127",
    "price_currency": "USD",
    .
    .
    .

i am using this example or tutorial for this

and in that its calling json from this url

an dover there he is parsing data with this json object jsonarray = jsonobject.getJSONArray("worldpopulation");

so i am getting a data from a url as above how to parse this much of json data in that i want only 2 or 3 fields of json array,.

Please help ,.. with a example which is working,..for above,..

推荐答案

You can use gson library. There is an example here!

其他推荐答案

As it is nested json and You have not specified which fields do you need its difficult to answer so Use this class as a refrence class use method from where You need data to be displayed, It will return the first object of your array.

public class Parser {

    public String getData() {
        String content = null;

        String result = "";
        InputStream is = null;

        // http get
        try {
            HttpClient httpclient = new DefaultHttpClient();

            String webUrl = "your url";

            HttpGet httppost = new HttpGet(webUrl);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();

            is = entity.getContent();

        } catch (Exception e) {
            Log.d("LOGTAG", "Error in http connection " + e.toString());
        }

        // convert response to string
        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();
            result = sb.toString();
            Log.d("LOGTAG", "Result :" + result);
        } catch (Exception e) {
            Log.d("LOGTAG", "Error converting result " + e.toString());
        }

        // parse json data

        try {

            JSONArray jarray = new JSONArray(result);
            JSONObject jobj = jarray.getJSONObject(jarray.getInt(0));
            content = jobj.getString("key");//key=name of the field you require
        } catch (Exception e) {

            Log.d("LOGTAG", e.toString());

        }
        return content;
    }
}

其他推荐答案

I saw your comment ... you do not want to change your code. but parsing a json yourself is pretty slow in comparison to using Gson..and use of gson is so simple you just need to create simple pojo classes and it will convert whole json into java object which you can use anywhere. this is also a neat way of doing it.here is an example

相关标签/搜索