Java和android中的字符串解析

Java和android中的字符串解析,java,string,parsing,Java,String,Parsing,我想用java解析下面的数据。我应该采取什么办法? 我想忽略内部{} 因此,版本,内容,提供,用户配置和图标作为名称和相应值 Version:"1"; Content:2013091801; Provide:"Airtel"; UserConfig : { Checksum = "sha1-234448e7e573b6dedd65f50a2da72245fd3b"; Source = "content\\user.ini"; }; Icon: { Checksum = "sha1

我想用java解析下面的数据。我应该采取什么办法? 我想忽略
内部
{}

因此,
版本
内容
提供
用户配置
图标
作为名称和相应值

Version:"1";
Content:2013091801;
Provide:"Airtel";
UserConfig : 
{
  Checksum = "sha1-234448e7e573b6dedd65f50a2da72245fd3b";
  Source = "content\\user.ini";
};
Icon: 
{
  Checksum = "sha1-a99f835tytytyt3177674489770e613c89390a8c4";
  Source = "content\\resept_ico.bmp";
};

这里我们不能使用
String.split(;”)
函数。

JSON在这种情况下听起来容易得多

。。但是,如果要使用正则表达式执行此操作,一种方法是:

对于简单的情况(如版本):

对于较难的(多行)情况(如UserConfig):


编辑:这可能是一种更简单的方法

// split the input string into fields
String[] fields = bigString.split("[^:]+:([^{;]+;)|({[^}]+};)");

// for each key-value pair
for(String field : fields) {
    // the key and value are separated by colons
    String parts = field.split(":");

    String key = parts[0];
    String value = parts[1];

    // do something with them, or add them to a map
    ...
}
最后一种方法基于每个键值对由以下内容组成的假设拆分输入字符串:

  • 开头有一些(非冒号)字符,后跟
  • 冒号
  • 或者

    ->某些字符不是大括号或分号(对于简单属性),或

    ->包含一些非大括号字符的大括号

  • 分号
  • 以下是json解决方案

        str = "{" + str.substring(0, str.lastIndexOf(";")).replace(";\n}", "}") + "}";
        try {
            JSONObject json = new JSONObject(str);
            String version = json.getString("Version");
            JSONObject config = json.getJSONObject("UserConfig");
            String source = config.getString("Source");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    
    因为“;”不应该在“}”前面


    我们需要删除它们

    如果使用
    Regex
    进行转换,然后创建一个方法来提取所需字段,
    我所做的是将上述输入转换为与Json兼容的字符串,然后用于将
    字符串
    解析为我的自定义类

    class MyVer
    {
        String Version;
        long Content;
        String Provide;
        Config UserConfig;
        Config Icon;
        String Source;              
    }
    
    class Config
    {
        String Checksum;
        String Source;          
    }
    
    public static void main(String[] args) 
    {
        String s = "Version:\"1\";Content:2013091801;Provide:\"Airtel\";UserConfig :{  Checksum = \"sha1-234448e7e573b6dedd65f50a2da72245fd3b\";  Source = \"content\\user.ini\";};Icon:{  Checksum = \"sha1-a99f835tytytyt3177674489770e613c89390a8c4\";  Source = \"content\\resept_ico.bmp\";};";                
    
        String startingBracePattern = Pattern.quote("{");
        String endBracePattern = Pattern.quote("}");
        s=s.replaceAll(Pattern.quote("\\"), "\\\\\\\\"); //Replacing all the single \ with double \\
        s = s.replaceAll("\\s*"+startingBracePattern +"\\s*", "\\{\""); //Replacing all the `spaces { spaces` with  `{"`  MEANS all the { to replace with {"
        s = s.replaceAll(";\\s*"+endBracePattern +"\\s*;", "\\};"); //Replacing all the `; spaces } spaces ;` with  `},"` MEANS all the ;}; to replace with };
    
        s = "{\"" + s.substring(0, s.length() - 1) +"}";    //Removing last ; and appending {" and }        
        s = s.replaceAll("\\s*:", "\":");                   // Replacing all the `space with :` with `":`       
        s = s.replaceAll("\\s*;\\s*", ",\"");               //Replacing all the `spaces ; spaces` with `,"`
        s = s.replaceAll("\\s*=\\s*", "\":");               //Replacing all the `spaces = spaces` with  `":`
    
        Gson gson = new Gson();
        MyVer newObj = gson.fromJson(s, MyVer.class);
    }
    
    这将转换并为您提供
    MyVer
    的对象,然后您可以访问所有变量


    注意:如果输入变量中存在所有的
    \r\n
    ,则可以稍微修改代码以替换它们。为了简单起见,我没有使用它们,也没有在一行中使用您的实际数据。

    这不是xml,所以我删除了标记。我想您可以使用正则表达式来实现这一点。只需使用这些关键字找到所需的值即可。“;”如果我使用正则表达式,{}的内部存在问题。删除最后一个“;”并将“{'}”添加到开始和结束。然后它变成一个json字符串,使用JsonObject来表达它非常感谢Ivan的回复,我会检查你的建议。
        str = "{" + str.substring(0, str.lastIndexOf(";")).replace(";\n}", "}") + "}";
        try {
            JSONObject json = new JSONObject(str);
            String version = json.getString("Version");
            JSONObject config = json.getJSONObject("UserConfig");
            String source = config.getString("Source");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    
       Source = "content\\resept_ico.bmp";
    }
    
    class MyVer
    {
        String Version;
        long Content;
        String Provide;
        Config UserConfig;
        Config Icon;
        String Source;              
    }
    
    class Config
    {
        String Checksum;
        String Source;          
    }
    
    public static void main(String[] args) 
    {
        String s = "Version:\"1\";Content:2013091801;Provide:\"Airtel\";UserConfig :{  Checksum = \"sha1-234448e7e573b6dedd65f50a2da72245fd3b\";  Source = \"content\\user.ini\";};Icon:{  Checksum = \"sha1-a99f835tytytyt3177674489770e613c89390a8c4\";  Source = \"content\\resept_ico.bmp\";};";                
    
        String startingBracePattern = Pattern.quote("{");
        String endBracePattern = Pattern.quote("}");
        s=s.replaceAll(Pattern.quote("\\"), "\\\\\\\\"); //Replacing all the single \ with double \\
        s = s.replaceAll("\\s*"+startingBracePattern +"\\s*", "\\{\""); //Replacing all the `spaces { spaces` with  `{"`  MEANS all the { to replace with {"
        s = s.replaceAll(";\\s*"+endBracePattern +"\\s*;", "\\};"); //Replacing all the `; spaces } spaces ;` with  `},"` MEANS all the ;}; to replace with };
    
        s = "{\"" + s.substring(0, s.length() - 1) +"}";    //Removing last ; and appending {" and }        
        s = s.replaceAll("\\s*:", "\":");                   // Replacing all the `space with :` with `":`       
        s = s.replaceAll("\\s*;\\s*", ",\"");               //Replacing all the `spaces ; spaces` with `,"`
        s = s.replaceAll("\\s*=\\s*", "\":");               //Replacing all the `spaces = spaces` with  `":`
    
        Gson gson = new Gson();
        MyVer newObj = gson.fromJson(s, MyVer.class);
    }