gpt4 book ai didi

android - Android中具有相同字段名的JSONArray解析和值映射

转载 作者:行者123 更新时间:2023-11-29 19:15:31 26 4
gpt4 key购买 nike

我有一个可以解析的 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"

最佳答案

您正在获取数组(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");

关于android - Android中具有相同字段名的JSONArray解析和值映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43639736/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com