2016-10-23 61 views
-2

当前我正在使用Iterator循环来解析我的JSON对象。适用于中小型数据集。但是当我的数据集增加时,表现时间也会增加。例如:如何使用For循环或While循环解析JSONObjects

//groupedData is my JSONObject dataset 
 
Iterator<String> iter = groupedData.keys(); 
 
      int i = 0; 
 
      while (iter.hasNext()) { 
 
       String key = iter.next(); 
 
       try { 
 
        Object value = groupedData.get(key); 
 
        //add the value to an entry 
 
        values_1.add(new Entry(Float.valueOf(String.valueOf(value)), i)); 
 
        //add key to an arraylist 
 
        labels_1.add(key); 
 
       } catch (JSONException e) { 
 
        // Something went wrong! 
 
       } 
 
       i++; 
 
      }

我使用了这些迭代环路,用于像4个更多种不同的数据集。它最终将永无止境地循环一切。

挖后,我得到了这个导致which showed results show that the Iterator mechanism is the slowest method to loop through a list. There's not much performance difference between the For and the While loop.

那么,有没有使用for或者while通过JSON对象循环更好的办法?

我JSONObject的数据是这样的:

{ 
 
    "Admin": 19900, 
 
    "James": 2000, 
 
    "Steve": 1501, 
 
    "Linda": 3840, 
 
    "Grace": 2170, 
 
    "Jeff": 1200, 
 
    "Carolle": 350 
 
}

有什么建议?

+0

你有没有尝试使用'GSON'?我建议你试试看。 –

+0

我还没试过,介意分享一个例子? –

+0

不要制作你自己的JSON解析器,那里有类似GSON或JACKSON的库。 –

回答

0

使用GSON解析JSON

试试这个,

Type type = new TypeToken<Map<String, String>>(){}.getType(); 
Map<String,String> map = new Gson().fromJson(groupedData.toString(), type);