Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在textview中将json数组显示为html_Android - Fatal编程技术网

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

Android 如何在textview中将json数组显示为html,android,Android,我想在mycode下面的html标记中显示json im getting json并在两个不同的文本视图中显示,但我想在单行中显示并显示所有getJSONObject(i).getString(“name”);粗体颜色的名称和所有getJSONObject(i).getString(“sub_成分”);在简单文本中不想使用两个文本视图我想在单个文本视图中显示什么?我想显示为html标记 "dish_ingredient": [ {"name":"Salt","

我想在mycode下面的html标记中显示json im getting json并在两个不同的文本视图中显示,但我想在单行中显示并显示所有getJSONObject(i).getString(“name”);粗体颜色的名称和所有getJSONObject(i).getString(“sub_成分”);在简单文本中不想使用两个文本视图我想在单个文本视图中显示什么?我想显示为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);


    }
“菜肴配料”:
[
{“名称”:“盐”,“sub_成分”:“},
{“名称”:“芝麻”、“sub_配料”:“},
{“名称”:“硫酸钙”,“sub_成分”:“},
{“名称”:“红糖”,“sub_配料”:“},
{“名称”:“盐”,“sub_成分”:“},
{“名称”:“汉堡包”、“sub_配料”:“},
{“名称”:“奶酪培养巴氏杀菌牛奶”,“sub_成分”:“},
{“名称”:“汉堡包包”、“sub_配料”:“小麦、烟酸、鸡蛋”}]}
最终线性布局表3=(线性布局)findViewById(R.id.table3);
JSONArray学校5=json2.getJSONArray(“菜肴配料”);
对于(int i=0;i<5.length();i++){
row4=GetLayoutFlater()。充气(R.layout.row2,null);
((TextView)row4.findviewbyd(R.id.name)).setText(school5
.getJSONObject(i).getString(“名称”);
((文本视图)第4行findViewById(R.id.SubComponents))
.setText(school5.getJSONObject(i).getString(“sub_成分”);
表3.addView(第4行);
}

如果我理解正确,您希望在一个
TextView
中显示上述格式的
JSON
,但要有一定的突出显示。退房如果您想保持
JSON
的原始格式,我只需获取初始的
JSONArray
,调用toString(),然后用您想要的格式文本替换单词“name”和“sub-component”

如果我误解了,您想提取每个
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);
//将其格式化为您想要的样子
SpanableString Name=“名称:”
SpanableString subIngredientTitle=“子成分:”;
字符串concatProduct=“”;
int arraySize=jsonArray.length();
for(int i=0;i

类似这样的东西可以让您处理
JSONArray
,并构建一个字符串,您可以将其设置为一个
TextView
。格式由您决定。

您可能需要调整它,我刚刚在答案框中写下了What is SpanableStrings用于设置文本格式。谷歌。这样,你可以有一个文本视图,不同的单词,不同的颜色,或者其他我如何在每个forloop中使字符串名称加粗,在最终文本视图后,所有“name”元素都显示加粗???只有在获取所有字符串后,在文本视图中加载时才告诉我如何使这个字符串加粗“name”