Android中使用相同字段名称的JSONArray解析和值映射

时间:2017-04-26 16:25:14

标签: android arrays json textview

我有一个JSONArray,我能够解析,但由于数组的结构,我很难映射从具有相同名称的字段中提取的值。而不是for循环一个接一个地分配值,赋值查看字段名称的最后一个实例。因此,第二个JSONObject的值被分配两次。

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

代码:

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);
                        }
                    });
                }

结果:

"-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 个答案:

答案 0 :(得分:1)

使用getter和setter为您的JSONArray键创建一个类。 使用for循环读取值,请参阅代码。

            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);
            }
        }

演员是班级名称。 使用适配器类在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
}

}

答案 1 :(得分:1)

您正在获取数组(i = 0)并在所有18个变量中写入,然后取值1并覆盖相同的18个变量。

您必须将此变量与循环中的变量分开。例如:当Group.getObj(i = 0)时。

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

然后当Group.getObj(i = 1):

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