有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓在Java中将JSON数组解析为字符串输出(Echo-Nest)

我似乎很难理解如何将嵌套的JSON数组数据解析为字符串。我试图通过JSONArray解析它们,而我的数据来自URI,所以我不能使用内联数据(而且太笨了,无法学习如何使用GSON)

我正在尝试获取一个艺术家列表,其中只包含他们的姓名字段。我只能输出Artister数组,但它们包含每个ID和名称的所有json值。我只需要名称字段。我知道有一种方法可以从艺术家数组中只获取名称字段,但我不知道获取它的语法

提前谢谢

以下是我到目前为止的情况:

JSONObject resultObject = new JSONObject(result);
JSONObject responseObject = resultObject.getJSONObject("response");
JSONArray artistArray = responseObject.getJSONArray("artists : name");
String nameString1 = artistArray.getString(0);
String nameString2 = artistArray.getString(1);
String nameString3 = artistArray.getString(2);
String nameString4 = artistArray.getString(3);
String nameString5 = artistArray.getString(4);
Button output1 = (Button)getView().findViewById(R.id.button1);
Button output2 = (Button)getView().findViewById(R.id.button2);
Button output3 = (Button)getView().findViewById(R.id.button3);
Button output4 = (Button)getView().findViewById(R.id.button4);
Button output5 = (Button)getView().findViewById(R.id.button5);
output1.setText(nameString1);
output2.setText(nameString2);
output3.setText(nameString3);
output4.setText(nameString4);
output5.setText(nameString5);

来自Echo嵌套URL的我的JSON数据

{"response": {"status": {"version": "4.2", "code": 0, "message": "Success"}, "artists": [{"id": "AR0PK561187B9B9EF9", "name": "TV on the Radio"}, {"id": "ARH6W4X1187B99274F", "name": "Radiohead"}, {"id": "ARAKQSI1257509D1DC", "name": "Rave Radio"}, {"id": "ARYCW5M1187B98DB6A", "name": "Radical Face"}, {"id": "ARVJWUX14801150165", "name": "Radio Doria"}]}}


共 (2) 个答案

  1. # 1 楼答案

    这就是我最后编写的代码,因为我很懒,精疲力竭。谢谢@cYrixmorten

                    JSONObject resultObject = new JSONObject(result);
                    JSONObject responseObject = resultObject.getJSONObject("response");
                    JSONArray artistArray = responseObject.getJSONArray("artists");
                    for (int i = 0; i < artistArray.length(); i++) {
                        JSONObject artist1 = artistArray.getJSONObject(0);
                        JSONObject artist2 = artistArray.getJSONObject(1);
                        JSONObject artist3 = artistArray.getJSONObject(2);
                        JSONObject artist4 = artistArray.getJSONObject(3);
                        JSONObject artist5 = artistArray.getJSONObject(4);
                        String name1 = artist1.getString("name");
                        String name2 = artist2.getString("name");
                        String name3 = artist3.getString("name");
                        String name4 = artist4.getString("name");
                        String name5 = artist5.getString("name");
                        Button button1 = (Button)getView().findViewById(R.id.button1);
                        Button button2 = (Button)getView().findViewById(R.id.button2);
                        Button button3 = (Button)getView().findViewById(R.id.button3);
                        Button button4 = (Button)getView().findViewById(R.id.button4);
                        Button button5 = (Button)getView().findViewById(R.id.button5);
                        button1.setText(name1);
                        button2.setText(name2);
                        button3.setText(name3);
                        button4.setText(name4);
                        button5.setText(name5);
                    }
    
  2. # 2 楼答案

    好的,我试试看。不是在我正在开发的计算机上,所以无法测试它:

    起点:

    {"response": {"status": {"version": "4.2", "code": 0, "message": "Success"}, "artists": [{"id": "AR0PK561187B9B9EF9", "name": "TV on the Radio"}, {"id": "ARH6W4X1187B99274F", "name": "Radiohead"}, {"id": "ARAKQSI1257509D1DC", "name": "Rave Radio"}, {"id": "ARYCW5M1187B98DB6A", "name": "Radical Face"}, {"id": "ARVJWUX14801150165", "name": "Radio Doria"}]}}

    JSONObject resultObject = new JSONObject(result);
    JSONObject responseObject = resultObject.getJSONObject("response");
    

    结果如下:

    {"status": {"version": "4.2", "code": 0, "message": "Success"}, "artists": [{"id": "AR0PK561187B9B9EF9", "name": "TV on the Radio"}, {"id": "ARH6W4X1187B99274F", "name": "Radiohead"}, {"id": "ARAKQSI1257509D1DC", "name": "Rave Radio"}, {"id": "ARYCW5M1187B98DB6A", "name": "Radical Face"}, {"id": "ARVJWUX14801150165", "name": "Radio Doria"}]}

    JSONArray artistArray = responseObject.getJSONArray("artists");
    

    应提供:

    [{"id": "AR0PK561187B9B9EF9", "name": "TV on the Radio"}, {"id": "ARH6W4X1187B99274F", "name": "Radiohead"}, {"id": "ARAKQSI1257509D1DC", "name": "Rave Radio"}, {"id": "ARYCW5M1187B98DB6A", "name": "Radical Face"}, {"id": "ARVJWUX14801150165", "name": "Radio Doria"}]

    你已经很接近了,在这一点上,我会迭代数组,因为我假设你无法事先知道你得到了多少结果:

    for (int i = 0; i < artistArray.length(); i++) {
       JSONObject artist = artistArray.getJSONObject(i);
    }
    

    对于索引0,这是:

    {"id": "AR0PK561187B9B9EF9", "name": "TV on the Radio"}

    现在唯一缺少的就是获取名称:

    for (int i = 0; i < artistArray.length(); i++) {
       JSONObject artist = artistArray.getJSONObject(i);
       String name = artist.getString("name");
       // generate button 
    }
    

    总结代码:

    JSONObject resultObject = new JSONObject(result);
    JSONObject responseObject = resultObject.getJSONObject("response");
    JSONArray artistArray = responseObject.getJSONArray("artists");
    for (int i = 0; i < artistArray.length(); i++) {
       JSONObject artist = artistArray.getJSONObject(i);
       String name = artist.getString("name");
       // generate button 
    }
    

    我认为动态生成按钮是有意义的,因为我假设您不知道会得到多少结果。否则,只需将名称存储在单独的列表中,并使用该名称列表显示输出