1

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.

"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:

try {
                JSONObject reader = new JSONObject(JSON_DATA);

                JSONObject PricingTier = reader.getJSONObject("PricingTier");

                JSONArray Group = PricingTier.getJSONArray("Group");

                for (int i = 0; i < 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);
                        }
                    });
                }

Result:

"-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"

2 Answers 2

2

You're getting array(i = 0) and writing in all the 18 variables then taking the value 1 and overwriting this same 18 variables.

You have to separate this variables from same for looping. for example: when Group.getObj(i = 0).

final String Type = g.getString("@Type");

and then when Group.getObj(i = 1):

final String TypeA = g.getString("@Type");
Sign up to request clarification or add additional context in comments.

2 Comments

When I try this, my fragment.getActivity thread cannot access the strings received. Is there a way to fix this?
Ah thank you! I simply put the runOnUiThread inside the (if == 0) and (if == 1) and it is working fine.
1

Create a class with getter and setter for your JSONArray Keys. Use a for loop to read the Values, Refer the code.

            for (int i = 0; i < Group.length(); i++) {
                JSONObject g = Group.getJSONObject(i);
                Actors actors = new Actors();
                actors.setType(g.getString("@Type"));
                ......//your remaining code
                arrayList.add(actors);
            }
        }

Actors is the class name. Use an adapter class to show the list in ListView.

public class yourAdapter extends ArrayAdapter<Actors> {

LayoutInflater vi;
ViewHolder holder;
int resource;
private final Activity context;
private final ArrayList<Actors> details;

public WifiAdapter(Activity context, int resource , ArrayList<Actors> details) {
    super(context, resource, details);
    vi =  (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.context = context;
    this.resource = resource;
    this.details = details;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if(v == null){
        holder = new ViewHolder();
        v = vi.inflate(resource, null);
        holder.Type = (TextView) v.findViewById(R.id.type);
        .......//your remaining code
        v.setTag(holder);
    }
    else {
        holder =(ViewHolder) v.getTag();
    }


    holder.Name.setText(details.get(position).getType());
    ..........//ur remaining code 


    return v;
}

static class ViewHolder {
    public TextView Type;
    //your remaing code
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.