如何将两个字符串解析为一个textview

时间:2018-06-03 05:23:14

标签: android json parsing

我希望我的TextView包含2个JSON数据字符串。

example.json

{
 "volumeInfo": {
   "title": "Computer Architecture",
   "subTitle": "A Quantitative Approach"
}

所以,从那个JSON我想要我的TextView:计算机架构:定量方法。我应该怎么做?感谢。

  

我只想使用一个TextView

3 个答案:

答案 0 :(得分:1)

就这样做

String title = "Computer Architecture";
String subtitle = "A Quantitative Approach";
TextView.setText(title + ": " + subtitle);

答案 1 :(得分:0)

您可以连接字符串,然后将其设置为TextView,如下所示:

 String title = "Computer Architecture";
 String subtitle = "A Quantitative Approach";
 yourTextView.setText(title + "," + subtitle);

答案 2 :(得分:-1)

//Please check the below code will help you as you want to set the text.

String response = "{" +
        " \"volumeInfo\": {" +
        "   \"title\": \"Computer Architecture\"," +
        "   \"subTitle\": \"A Quantitative Approach\"}" +
        "}";   
 try {
        String title = "", subTitle = "";
        JSONObject jsonObject = new JSONObject(response);
        if (jsonObject.has("volumeInfo")) {
            JSONObject jsonObject1 = jsonObject.getJSONObject("volumeInfo");
            if (jsonObject1.has("title")) {
                title = jsonObject1.getString("title");
            }
            if (jsonObject1.has("subTitle")) {
                subTitle = jsonObject1.getString("subTitle");
            }
            String combineString = title + ": " + subTitle;
            TextView textView = findViewById(R.id.textView);
            textView.setText(combineString);

        }
    } catch (JSONException e) {
        e.printStackTrace();
    }