【问题标题】:search text for strings in between strings android在字符串之间搜索字符串 android
【发布时间】:2016-01-08 04:12:34
【问题描述】:

我有很长的JSON 文本,我必须在其中找到介于以下之间的电影标题字符串:

(((((((   .","title":" AND ","type":"   )))))))

我已经阅读了其他解决方案,例如:

   String mydata = json;
               Pattern pattern = Pattern.compile(".\",\"title\":\"(.*?)\",\"type\":\"");   
             Matcher matcher = pattern.matcher(mydata);
               while (matcher.find())
             {
                 testare += (matcher.group(1));
        }

还有更混乱的方式:

    List<String> strings = Arrays.asList( json.replaceAll("^.*?.\",\"title\":\"", >>"")
                    .split("\",\"type\":\".*?(.\",\"title\":\"|$)"));
            ArrayAdapter<String> myAdapter = new
                    ArrayAdapter<String>(MainActivity.this,
                    android.R.layout.simple_list_item_1, strings);
            final ListView myList = (ListView) findViewById(R.id.listVyn);
            myList.setAdapter(myAdapter);

Json 包含JSON 中的整个文本作为字符串。 我的问题是,当我搜索 Dirty 时,它会推荐包含“Dirty”的电影,我想搜索字符串并挑选出所有标题。但由于某种原因,它似乎循环了第一个电影名称,即“Dirty”,然后忽略了其余的。我该怎么办?

【问题讨论】:

    标签: java android json regex string


    【解决方案1】:

    如果我的理解正确,我可能会建议一种不同的方法。如果您将 json 对象反序列化为 java 对象,您应该会更容易发现问题。即,您将使用一个包含来自 json 的信息的 java 对象,而不是使用 json。您可以通过多种方式执行此操作,例如使用 Gson:

    Gson gson = new Gson();
    MyObject myObject = gson.fromJson(jsonString, MyObject.class);
    

    完成此操作后,您可以使用 myObject 从对象中获取文本。例如,myObject.getTitle()。如果您想在 that 字符串上使用正则表达式,那就太好了。但我不会在 json 上使用正则表达式。

    为了创建代表 json 的 MyObject.class,您可以使用: http://pojo.sodhanalibrary.com

    【讨论】:

    • 您好,感谢您的快速答复。我使用的 Json 变量是一个 java 字符串。所以我搜索了整个json,将json中的所有文本都放到了字符串中,然后尝试搜索字符串。所以我认为我已经在 java 中工作了,不是吗?
    猜你喜欢
    • 1970-01-01
    • 2022-01-11
    • 2017-10-29
    • 2011-01-30
    • 1970-01-01
    • 2016-11-30
    相关资源
    最近更新 更多