如何在textview中将json数组显示为html

时间:2013-10-08 13:10:09

标签: android

我想在mycode下面的html标签中显示json即时获取json并在两个不同的textview中显示但我希望以单行显示并显示所有getJSONObject(i).getString(“name”));粗体颜色的名称和所有getJSONObject(i).getString(“sub_ingredients”));在简单的文字中不想使用两个textview我想toshow insingle文本查看什么会ido?我想显示为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);


    }

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您希望在一个JSON中显示上面格式化的TextView,但需要突出显示。查看this post。如果你想保持JSON的原始格式,我会得到最初的JSONArray,调用toString(),然后将文字替换为“name”和“sub-ingredient”。你想要的方式。

如果我误解了您想要提取每个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并构建一个可以设置为TextView的字符串。格式由您决定。