android - 在为无名 JSONObject 解析 JSONArray 时如何停止 for 循环?

标签 android json parsing for-loop

我想解析一个 JSONArray,其中包含没有名称的 JSONObject,并且其 (index int) 在数组中的位置每次都会更改一周左右。我试图通过它的属性来解析特定的 Object,但我的解析器只返回数组中的最后一个 Object。

当循环到达我要解析的对象并确定对象的 int 索引以供进一步解析时,我如何停止循环。

try {
        JSONArray jArray = JSONthing.getJSONfromURL("http://something.com");
        String attributeiwant = "abc";
        for (int i = 0; i < jArray.length(); i++) {
            JSONObject alpha = jArray.getJSONObject(i);
            String attributeparsed = alpha.getString("widget");
            if (attributeparsed == attributeiwant) {
                //determine int index of object, so i can parse other attributes
                //from same object          

            }
        }
        } catch (Exception e) {
        Log.e("log_tag", "Error parsing data "+ e.toString());
        }

最佳答案

使用 String.equals 来比较字符串而不是 ==

try {
        JSONArray jArray = JSONthing.getJSONfromURL("http://something.com");
        String attributeiwant = "abc";
        for (int i = 0; i < jArray.length(); i++) {
            JSONObject alpha = jArray.getJSONObject(i);
            String attributeparsed = alpha.getString("widget");
            if (attributeparsed.equals(attributeiwant)) {
                //determine int index of object, so i can parse other attributes
                //from same object          
                // Get data from JsonObject
                break;
            }
        }
        } catch (Exception e) {
        Log.e("log_tag", "Error parsing data "+ e.toString());
        }

关于android - 在为无名 JSONObject 解析 JSONArray 时如何停止 for 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11733178/

相关文章:

android - 安全错误 : DOM Exception 18 When opening database in WebView

javascript - Angular .js :38 Uncaught Error: [$injector:modulerr]

json - 在 PowerShell 的 jq 中使用 $ 变量

php: 从 html 中解析字符串

Android Tabhost 内容在平板电脑上卡住但在模拟器中不卡住

android - 将旧版本的 SQLite 数据库复制到较新版本的应用程序时如何避免崩溃

php - 安卓 + PHP + MySQL

java - 在 Java 中将 bean 转换为 Json

javascript - 使用 Javascript 解析 wiki 模板调用

c - XML 到 C 结构和 C 结构到 XML