更快的在android系统解析一个JSON字符串的方法 [英] Faster way to parse a JSON String in android

查看:142
本文介绍了更快的在android系统解析一个JSON字符串的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个方法来分析JSON字符串,但实在是太慢了...有没有更好的办法做到这一点?
谢谢

 同步私人无效parseCategories(字符串响应){    尝试{
        JSONArray categoriesJSONArray =新JSONArray(响应);
        //通过所有联系人循环
        的for(int i = 0; I< categoriesJSONArray.length();我++){
            JSONObject的currentCategory = categoriesJSONArray.getJSONObject(I)            字符串标签=;
            字符串categoryId =;
            //存储在变量中的每个JSON项目
            如果(currentCategory.has(标签))
                标签= currentCategory.getString(标签);
            如果(currentCategory.has(ID))
                的categoryId = currentCategory.getString(ID);            如果(
               LABEL =空&安培;!&安培;
               的categoryId!= NULL
              )
            {
                类别TOADD =新类别(的categoryId,标签);
                categories.add(TOADD);
            }        }        //字母顺序
        Collections.sort(
                类,
                新的比较<饲料和GT;(){
                    公众诠释比较(LHS饲料,饲料RHS){
                        返回lhs.getTitle()的compareTo(rhs.getTitle())。
                    }
                }
        );        意向意图=新意图(CategoriesLoaded);
        LocalBroadcastManager.getInstance(mAppContext).sendBroadcast(意向);
    }赶上(JSONException E){
        e.printStackTrace();
    }}


解决方案

下面我们尝试以下code开始。您需要 GSON 库吧。

  GSON GSON =新GSON();
MyBean myBean = gson.fromJson(响应);

注:在这里MyBean类包含的字段present在您例如JSON字符串 ID ,以及getter和setter方法​​。所有的休息是由 GSON 处理。

下面是一个简单的演示。

 进口com.google.gson.annotations.SerializedName;公共类箱{  @SerializedName(ID)
  私人字符串categoryId;  // getter和setter
}

假设你 JSON 看起来如下:

  {ID:A12}

您可以按如下解析:

 类解析{
 公共无效parseJson(字符串响应){
  GSON GSON =新GSON();
  盒包装盒= gson.fromJson(响应,Box.class);
  的System.out.println(box.getCategoryId());
 }
}

输出:

  A12

更多关于 GSON 访问这里

I'm using this method to parse a JSON string, but it is too slow... is there a better way to do it? Thanks

synchronized private void parseCategories(String response){

    try{
        JSONArray categoriesJSONArray = new JSONArray (response);


        // looping through All Contacts
        for(int i = 0; i < categoriesJSONArray.length(); i++){
            JSONObject currentCategory = categoriesJSONArray.getJSONObject(i);

            String label="";
            String categoryId="";


            // Storing each json item in variable
            if(currentCategory.has("label"))
                label = currentCategory.getString("label");


            if(currentCategory.has("id"))
                categoryId = currentCategory.getString("id");

            if(
               label!=null &&
               categoryId!=null
              )
            {
                Category toAdd = new Category(categoryId, label);
                categories.add(toAdd);
            }

        }

        //Alphabetic order
        Collections.sort(
                categories,
                new Comparator<Feed>() {
                    public int compare(Feed lhs, Feed rhs) {
                        return lhs.getTitle().compareTo(rhs.getTitle());
                    }
                }
        );

        Intent intent = new Intent("CategoriesLoaded");
        LocalBroadcastManager.getInstance(mAppContext).sendBroadcast(intent);


    }catch (JSONException e) {
        e.printStackTrace();
    }

}

解决方案

Here's try following code to start with. You would need Gson library for it.

Gson gson=new Gson();
MyBean myBean=gson.fromJson(response);

Note: Here MyBean class contains the fields present in you json string for e.g. id, along with getter and setters. Rest of all is handled by Gson.

Here's a quick demo.

import com.google.gson.annotations.SerializedName;

public class Box {

  @SerializedName("id")
  private String categoryId;

  // getter and setter
}

Say you JSON looks as following:

{"id":"A12"}

You can parse it as follows:

class Parse{
 public void parseJson(String response){
  Gson gson=new Gson();
  Box box=gson.fromJson(response,Box.class);
  System.out.println(box.getCategoryId());
 }
}

Output :

A12

For more on Gson visit here

这篇关于更快的在android系统解析一个JSON字符串的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆