【问题标题】:Android Parse JSON string to String arrayAndroid 将 JSON 字符串解析为字符串数组
【发布时间】:2014-07-21 02:30:59
【问题描述】:

我正在尝试解析从 Web 服务器传回的 json 字符串,并将其转换为键控字符串数组。结果希望看起来像“str[ID] = 艺术家 - 标题”。

这是我用来获取 json 并开始解析它的代码。

                new Thread(new Runnable() {
                public void run() {
                    try {
                        try {
                            HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client
                            HttpGet httpget = new HttpGet("http://somesite.net/some.php"); // Set the action you want to do
                            HttpResponse response = httpclient.execute(httpget); // Executeit
                            HttpEntity entity = response.getEntity();
                            InputStream is = entity.getContent(); // Create an InputStream with the response
                            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
                            StringBuilder sb = new StringBuilder();
                            String line = null;
                            while ((line = reader.readLine()) != null) // Read line by line
                                sb.append(line + "\n");


                            String resString = sb.toString(); // Result is here
                            JSONObject json = new JSONObject(resString);
                            String[] array = new Gson().fromJson(resString, String[].class);
                            Log.e("TESTAPP", "SOME RES E "  + array.toString());

                            is.close(); // Close the stream
                        }
                        catch (Exception e) {
                            Log.e("TESTAPP", "SOME Catch Error E "  + e.toString());
                        }
                    }
                    catch (Exception e){
                        Log.e("TESTAPP", "SOME Error" + e.getMessage());
                    }
                }
            }).start();

正在使用的一些示例 json

{"item":{"artist":"Billy Idol","requestid":"42207","title":"Rebel Yell"}}{"item":{"artist":"Black Sunshine","requestid":"42208","title":"Once In My Life"}}{"item":{"artist":"Blackstreet","requestid":"42209","title":"Before I Let You Go"}}{"item":{"artist":"Black Sabbath","requestid":"42210","title":"Time Machine"}}

【问题讨论】:

    标签: android arrays json object


    【解决方案1】:

    您尝试解析的 JSON 字符串中存在错误。 {} 中的每个字符串都是一个 JSON 对象,并且在提供的字符串中有一个包含另一个 item 对象的对象:

    {
      "item": {
        "artist": "Billy Idol",
        "requestid": "42207",
        "title": "Rebel Yell"
      }
    }
    

    然后只是另一个包含另一个item 对象的对象。

    {
      "item": {
        "artist": "Black Sunshine",
        "requestid": "42208",
        "title": "Once In My Life"
      }
    }
    

    这是一个无效的 JSONString。我建议您使用以下工具:http://jsonlint.com/ 在尝试解析之前验证您的 JSON 字符串。

    【讨论】:

    • 修复我的 json 后,正确解析要容易得多。谢谢!
    猜你喜欢
    • 2012-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多