title: JSONArray Parsing and Value Mapping with Same Field Names in Android
body: <p>I have a JSONArray which I am able to parse, but due to the structure of the array, I am having difficulties mapping the values extracted from fields with the same names. Instead of the for loop assigning values one after the other, the assignment looks at the last instance of the field name. So the values from the second JSONObject are assigned twice.</p>
  
  <pre><code>"Group": [
      {
        "-Type": "Mouldings",
        "CurTier": "BRZ",
        "NxtTier": "SIL",
        "CurTierFrom": "$4,000",
        "CurTierTo": "$9,999",
        "NxtTierFrom": "$10,000",
        "NxtTierTo": "$14,999",
        "CurSales": "$2,107",
        "ReqSales": "$7,893"
      },
      {
        "-Type": "Accessories",
        "CurTier": "BAS",
        "NxtTier": "GLD",
        "CurTierFrom": "$0",
        "CurTierTo": "$1,499",
        "NxtTierFrom": "$1,500",
        "NxtTierTo": "$4,999",
        "CurSales": "$693",
        "ReqSales": "$807"
      }
    ]
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>try {
                  JSONObject reader = new JSONObject(JSON_DATA);
  
                  JSONObject PricingTier = reader.getJSONObject("PricingTier");
  
                  JSONArray Group = PricingTier.getJSONArray("Group");
  
                  for (int i = 0; i &lt; Group.length(); i++) {
                      JSONObject g = Group.getJSONObject(i);
  
  
                      final String Type = g.getString("@Type");
                      final String CurTier = g.getString("CurTier");
                      final String NxtTier = g.getString("NxtTier");
                      final String CurTierFrom = g.getString("CurTierFrom");
                      final String CurTierTo = g.getString("CurTierTo");
                      final String NxtTierFrom = g.getString("NxtTierFrom");
                      final String NxtTierTo = g.getString("NxtTierTo");
                      final String CurSales = g.getString("CurSales");
                      final String ReqSales = g.getString("ReqSales");
                      final String TypeA = g.getString("@Type");
                      final String CurTierA = g.getString("CurTier");
                      final String NxtTierA = g.getString("NxtTier");
                      final String CurTierFromA = g.getString("CurTierFrom");
                      final String CurTierToA = g.getString("CurTierTo");
                      final String NxtTierFromA = g.getString("NxtTierFrom");
                      final String NxtTierToA = g.getString("NxtTierTo");
                      final String CurSalesA = g.getString("CurSales");
                      final String ReqSalesA = g.getString("ReqSales");
  
                      fragment.getActivity().runOnUiThread(new Runnable() {
                          @Override
                          public void run() {
  
                              tvType.setText(Type);
                              tvCurTier.setText(CurTier);
                              tvNxtTier.setText(NxtTier);
                              tvCurTierFrom.setText(CurTierFrom);
                              tvCurTierTo.setText(CurTierTo);
                              tvNxtTierFrom.setText(NxtTierFrom);
                              tvNxtTierTo.setText(NxtTierTo);
                              tvCurSales.setText(CurSales);
                              tvReqSales.setText(ReqSales);
                              tvTypeA.setText(TypeA);
                              tvCurTierA.setText(CurTierA);
                              tvNxtTierA.setText(NxtTierA);
                              tvCurTierFromA.setText(CurTierFromA);
                              tvCurTierToA.setText(CurTierToA);
                              tvNxtTierFromA.setText(NxtTierFromA);
                              tvNxtTierToA.setText(NxtTierToA);
                              tvCurSalesA.setText(CurSalesA);
                              tvReqSalesA.setText(ReqSalesA);
                          }
                      });
                  }
  </code></pre>
  
  <p>Result: </p>
  
  <pre><code>"-Type": "Accessories",
    "CurTier": "BAS",
    "NxtTier": "GLD",
    "CurTierFrom": "$0",
    "CurTierTo": "$1,499",
    "NxtTierFrom": "$1,500",
    "NxtTierTo": "$4,999",
    "CurSales": "$693",
    "ReqSales": "$807"
  "-Type": "Accessories",
    "CurTier": "BAS",
    "NxtTier": "GLD",
    "CurTierFrom": "$0",
    "CurTierTo": "$1,499",
    "NxtTierFrom": "$1,500",
    "NxtTierTo": "$4,999",
    "CurSales": "$693",
    "ReqSales": "$807"
  </code></pre>