从Java中的字符串替换子字符串

时间:2018-10-16 11:30:55

标签: java json string

我有字符串hello Mr $name ur score is $value,获得$ name和$ value部分的最佳方法是什么?

String json = "{\n" + "\"id\": 1,\n" + "\"data\":[\n" + "{\n"
            + "\"to\":123456789,\"name\":\"james\",\"value\":200\n" + "},\n" + "{\n"
            + "\"to\":123456789,\"name\":\"jhon\",\"value\":20\n" + "}]\n" + "}\n" + "";

Object obj = new JSONParser().parse(json);
            JSONObject jsonObject = (JSONObject) obj;
            long id = (long) jsonObject.get("id");
            JSONArray arrayOfdata = (JSONArray) jsonObject.get("data");
            JSONObject dataObject = new JSONObject();
            ArrayList<String> data = new ArrayList<>();
            for (String w : words) {
                if (w.contains("$"))
                    if (json.contains(w.substring(1))) {
                        data.add(w.substring(1));
                    }
            }
            for (int n = 0; n < arrayOfdata.size(); n++) {
                dataObject = (JSONObject) arrayOfdata.get(n);
                for (int j = 0; j < data.size(); j++) {
                    String msg = message.replace(data.get(j).toString(), dataObject.get(data.get(j)).toString());
                    String strNew = msg.replace("$", "");
                    logger.info("strNew " + strNew);
                }
            }

Result

2 个答案:

答案 0 :(得分:0)

public static void main(String...strings) {     
    String inputString = "{\n" + "\"id\": 1,\n" + "\"data\":[\n" + "{\n" + "\"to\":123456789,\"name\":\"james\",\"value\":200\n" + "},\n" + "{\n" + "\"to\":123456789,\"name\":\"jhon\",\"value\":20\n" + "}]\n" + "}\n" + "";
    Pattern pattern = Pattern.compile("(?:\"name\":\")(.*?)(?:\"value\":)[0-9]*");
    Matcher m = pattern.matcher(inputString);

    while (m.find()) {
        String[] matches = m.group().split(",");
        String name = null, value = null;
        for (String match : matches) {
            if(match.contains("name")){
                name= match.substring(match.indexOf("name")+"name".length()).replaceAll("\"", "");
            }else if(match.contains("value")) {
                value= match.substring(match.indexOf("value")+"value".length()).replaceAll("\"", "");
            }
        }
        System.out.println("Bonjour Mr. "+name+" votre score est value "+value);
    }       
}

答案 1 :(得分:0)

我有点了解您的代码的目的,这是尝试获取json数组中的每个条目,每个名称和值的方法,希望对您有所帮助。

String json = "{\n" + "\"id\": 1,\n" + "\"data\":[\n" + "{\n"
            + "\"to\":123456789,\"name\":\"james\",\"value\":200\n" + "},\n" + "{\n"
            + "\"to\":123456789,\"name\":\"jhon\",\"value\":20\n" + "}]\n" + "}\n" + "";

    JSONObject jsonObject = new JSONObject(json);
    JSONArray arrayOfdata = (JSONArray) jsonObject.get("data");

    String message = "hello Mr %s your score is %s";

    for (int i = 0; i < arrayOfdata.length(); i++) {
        JSONObject obj = arrayOfdata.getJSONObject(i);
        Object name = obj.get("name");
        Object value = obj.get("value");

        System.out.printf(message, name, value);
        System.out.println();
    }