Tengo la cadena hello Mr $name ur score is $value, ¿cuál es la mejor manera de obtener $ name y $ value part?

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

Resultado

0
the hosss 16 oct. 2018 a las 14:30

2 respuestas

La mejor respuesta
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);
    }       
}
0
Hades 16 oct. 2018 a las 15:56

Entiendo el objetivo de su código, aquí hay un intento de obtener para cada entrada en la matriz json, el nombre y los valores de cada uno, espero que ayude.

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();
    }
0
мυѕτавєւмo 16 oct. 2018 a las 12:57