簡體   English   中英

從Java中的字符串替換子字符串

[英]replace substring from string in java

我有字符串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);
                }
            }

結果

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);
    }       
}

我有點理解您的代碼的目的,這是嘗試獲取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();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM