-1

我想在我的代码下面的 html 标记中显示 json 我正在获取 json 并显示在两个不同的文本视图中,但我想以单行显示并显示所有 getJSONObject(i).getString("name")); 粗体名称和所有 getJSONObject(i).getString("sub_ingredients")); 在简单的文本中不想使用两个文本视图我想显示单个文本视图会做什么?我想显示为 html 标签

              "dish_ingredient":
  [
  {"name":"Salt","sub_ingredients":""},
  {"name":"Sesame Seeds","sub_ingredients":""},
   {"name":"Calcium Sulfate","sub_ingredients":""},
  {"name":"Brown Sugar","sub_ingredients":""},
  {"name":"Salt","sub_ingredients":""},
 {"name":"Hamburger Bun","sub_ingredients":""},
 {"name":"Cheese-cultured pasteurized milk","sub_ingredients":""},
 {"name":"Hamburger Bun","sub_ingredients":"Wheat, Niacin, Eggs"}]}




  final LinearLayout table3 = (LinearLayout) findViewById(R.id.table3);

JSONArray school5 = json2.getJSONArray("dish_ingredient");

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

   row4 = getLayoutInflater().inflate(R.layout.row2, null);
  ((TextView) row4.findViewById(R.id.name)).setText(school5

                    .getJSONObject(i).getString("name"));
((TextView) row4.findViewById(R.id.subingredients))

  .setText(school5.getJSONObject(i).getString( "sub_ingredients"));

table3.addView(row4);


    }
4

1 回答 1

0

如果我理解正确,您希望将JSON上述格式的内容合二为一,TextView但要突出显示。看看这篇文章。如果您想保持JSON其原始格式,我只需获取初始格式JSONArray,调用 toString(),然后用您想要的格式文本替换“名称”和“子成分”这两个词。

如果我误解了,并且您想提取每个值,JSONObject您只需要遍历JSONArray

//format these to look like whatever you want
SpanableString nameTitle = "Name: "
SpanableString subIngredientTitle = " Sub-ingredient: ";
String concatProduct = "";
int arraySize = jsonArray.length();

for(int i = 0; i < arraySize; i++){
    JSONObject thing = jsonArray.getJSONObject(i);
    String name = thing.getString("name");
    String subIngredient = thing.getString("sub-ingredient");
    if(i == 0){
        concatProduct = nameTitle + name + subIngredientTitle + subIngredient;
     } else {
        concatProduct += " " + nameTitle + name + subIngredientTitle + subIngredient;
     }
}

textView.setText(concatProduct);

像这样的东西可以让你处理JSONArray并构建一个你可以设置为 one 的字符串TextView。格式由您决定。

于 2013-10-08T13:26:47.837 回答