2017-04-11 85 views
1

我有一大堆jsonObjects的jsonArray,我想通過market_id進行分組,這樣具有類似market_id的對象分別存放在自己的列表或數組中。我怎樣才能做到這一點?在jsonArray中對jsonObjects進行分組Android

[ 
{ 
    "product_id": "12301", 
    "selection": "No", 
    "sales": "31", 
    "market_id": "10", 
}, 
{ 
    "product_id": "12302", 
    "selection": "No", 
    "sales": "24", 
    "market_id": "43", 
}, 
{ 
    "product_id": "12303", 
    "selection": "Yes", 
    "sales": "121", 
    "market_id": "10", 
}, 
{ 
    "product_id": "12304", 
    "selection": "No", 
    "sales": "0", 
    "market_id": "43", 
}, 
{ 
    "product_id": "12305", 
    "selection": "Yes", 
    "sales": "20", 
    "market_id": "43", 
}, 

]

爲了實現這樣的事情:

[{ 
    "product_id": "12304", 
    "selection": "No", 
    "sales": "0", 
    "market_id": "43", 
}, 
{ 
    "product_id": "12305", 
    "selection": "Yes", 
    "sales": "20", 
    "market_id": "43", 
}, 
{ 
    "product_id": "12302", 
    "selection": "No", 
    "sales": "24", 
    "market_id": "43", 
},] 
+0

我想你應該根據該先檢查ID您做出類似ID –

+0

你嘗試過什麼到目前爲止JSON陣列?顯示你的代碼。 –

+0

@Bmbariah如果你按market_id分組,你的product_id怎麼樣?在JSON中查看您的product_id對於每個market_id都是不同的。 –

回答

0

我最終只是通過在jsonArray每一個對象進行循環並補充說,有着相似的market_id的轉化爲自己jsonArray對象。它不漂亮,但它的工作原理。

try { 

     JSONArray jsonArray = new JSONArray(mainjson); 

     for (int i = 0; i < jsonArray.length(); i++) { 

      JSONObject jsonObject = jsonArray.getJSONObject(i); 

      String market_id = jsonObject.getString("market_id"); 

      if (market_id.equalsIgnoreCase("43")) { 

       JSONObject json = new JSONObject(); 
       json.put("match_id", jsonObject.getString("product_id")); 
       json.put("selection", jsonObject.getString("selection")); 
       json.put("sales", jsonObject.getString("sales")); 
       json.put("market_id", jsonObject.getString("market_id")); 
       new_json.put(json); 

      } else if (market_id.equalsIgnoreCase("10")) { 
       .... 
+0

等一下,我會爲您提供一個優雅的解決方案 – AlexTa

+0

謝謝!讓我測試一下。 – Bmbariah

+0

讓我知道它是否有效,我已經測試過,看起來很完美 – AlexTa

1

首先由marketId創建實現Comparator接口,以便讓您排序產品列表一個產品模型類,在這種情況下。

Product.java

public class Product implements Comparator<Product> { 
public String productId; 
public String selection; 
public String sales; 
public String marketId; 

public Product() { 
    super(); 
} 

@Override 
public int compare(final Product p1, final Product p2) { 
    if (!TextUtils.isEmpty(p1.marketId) && !TextUtils.isEmpty(p2.marketId)) { 
     return p1.marketId.compareTo(p2.marketId); //Ascending order 
    } 
    return 0; 
} 
} 

二,創建它分析你的產品列表JSONArray產品類型列表中,並從產品類型列表分組產品JSONArray一個產品解析器類。

ProductParser.java

public class ProductParser { 
private static final String TAG = ProductParser.class.getSimpleName(); 
private static final String PRODUCT_ID = "product_id"; 
private static final String SELECTION = "selection"; 
private static final String SALES = "sales"; 
private static final String MARKET_ID = "market_id"; 
private static final String HELPER_ID = "-1"; 

public ProductParser() { 
    super(); 
} 

public List<Product> parseProductArrayToProductList(final JSONArray productArray) { 
    final List<Product> productsList = new ArrayList<>(); 
    if (null != productArray) { 
     try { 
      final int productCount = productArray.length(); 
      for (int i = 0; i < productCount; ++i) { 
       final JSONObject productJson = productArray.getJSONObject(i); 
       final Product product = new Product(); 
       product.productId = productJson.getString(PRODUCT_ID); 
       product.selection = productJson.getString(SELECTION); 
       product.sales = productJson.getString(SALES); 
       product.marketId = productJson.getString(MARKET_ID); 
       productsList.add(product); 
      } 
     } catch (final JSONException e) { 
      Log.e(TAG, e.toString()); 
     } 
    } 
    return productsList; 
} 

public JSONArray parseProductListToGroupedProductArray(final List<Product> productList) { 
    final JSONArray groupedProductArray = new JSONArray(); 
    if (null != productList && !productList.isEmpty()) { 
     final int productCount = productList.size(); 
     String currentMarketId = HELPER_ID; 
     JSONArray productArray = null; 
     for (int i = 0; i < productCount; ++i) { 
      final Product product = productList.get(i); 
      if (null != product) { 
       if (!currentMarketId.equals(product.marketId)) { 
        currentMarketId = product.marketId; 
        if (null != productArray) { 
         groupedProductArray.put(productArray); 
        } 
        productArray = new JSONArray(); 
       } 
       try { 
        final JSONObject productObject = new JSONObject(); 
        productObject.put(PRODUCT_ID, product.productId); 
        productObject.put(SELECTION, product.selection); 
        productObject.put(SALES, product.sales); 
        productObject.put(MARKET_ID, product.marketId); 
        productArray.put(productObject); 
       } catch (final JSONException e) { 
        Log.e(TAG, e.toString()); 
       } 
      } 
     } 
     if (null != productArray) { 
      groupedProductArray.put(productArray); 
     } 
    } 
    return groupedProductArray; 
} 
} 

最後,在您的活動或任何你需要實現這個功能,使用提供clases。

MainActivity.java

public class MainActivity extends AppCompatActivity { 
private static final String TAG = MainActivity.class.getSimpleName(); 

@Override 
protected void onCreate(final Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    final String data = "\r\n\r\n[\r\n {\r\n  \"product_id\": \"12301\",\r\n  \"selection\": \"No\",\r\n  \"sales\": \"31\",\r\n  \"market_id\": \"10\"\r\n },\r\n {\r\n  \"product_id\": \"12302\",\r\n  \"selection\": \"No\",\r\n  \"sales\": \"24\",\r\n  \"market_id\": \"43\"\r\n },\r\n {\r\n  \"product_id\": \"12303\",\r\n  \"selection\": \"Yes\",\r\n  \"sales\": \"121\",\r\n  \"market_id\": \"10\"\r\n },\r\n {\r\n  \"product_id\": \"12304\",\r\n  \"selection\": \"No\",\r\n  \"sales\": \"0\",\r\n  \"market_id\": \"43\"\r\n },\r\n {\r\n  \"product_id\": \"12305\",\r\n  \"selection\": \"Yes\",\r\n  \"sales\": \"20\",\r\n  \"market_id\": \"43\"\r\n }\r\n]\r\n\r\n"; 
    final List<Product> productList = getProductList(data); 
    Collections.sort(productList, new Product()); 
    final JSONArray sortedProductArray = getProductArray(productList); 
    Log.e(TAG, sortedProductArray.toString()); // Here you have! 
} 

private List<Product> getProductList(final String data) { 
    List<Product> productList = new ArrayList<>(); 
    try { 
     final JSONArray productArray = new JSONArray(data); 
     final ProductParser parser = new ProductParser(); 
     productList = parser.parseProductArrayToProductList(productArray); 
    } catch (final JSONException e) { 
     Log.e(TAG, e.toString()); 
    } 
    return productList; 
} 

private JSONArray getProductArray(final List<Product> productList) { 
    final ProductParser parser = new ProductParser(); 
    return parser.parseProductListToGroupedProductArray(productList); 
} 
}