0

I have the string hello Mr $name ur score is $value, What is the best way to get $name and $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);
                }
            }

Result

azro
  • 53,056
  • 7
  • 34
  • 70
the hosss
  • 3
  • 2

2 Answers2

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);
    }       
}
Amit Kumar Lal
  • 5,537
  • 3
  • 19
  • 37
  • That's a good answer, but when the json is written differently your regex won't work, here you supposed that always name and value come like in the example, you may have name,sthElse, value ... – Mustapha Belmokhtar Oct 16 '18 at 13:07
  • @мυѕτавєւмo I have updated my answer with the requirements that you have mentioned, this works when even there is some key-value pair JSON data in-between name and value – Amit Kumar Lal Oct 16 '18 at 15:57
  • @the hosss will you please look at the answer provided and see if it serve your purpose – Amit Kumar Lal Oct 17 '18 at 06:35
0

I kind of understand the aim of your code, here is an attempt to get for each entry in the json array, the name and the values of each, I hope it helps.

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();
    }
Mustapha Belmokhtar
  • 1,231
  • 8
  • 21