-5

I have string with value like below

String s1 = "{"measurements": ["528391^MDC_DEV_SPEC_PROF^M12343"], "metrics": ["150021^MDC_PRESS_BLD_NONIN234^MDC", "150022^MDC_PRESS_BLD_NONINV_DIA^MDC", "150023^MDC_PRESS_BLD_12344^MDC", "149546^MDC_PULS_RATE_INV^MDC"], "accessoryTypes": [] }"

i need value of measurements only i.e my result should be : 528391^MDC_DEV_SPEC_PROF^M12343

Kindly help in solving this problem.

Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52
Hema Latha
  • 85
  • 1
  • 3
  • 12

5 Answers5

1

if your text is always in that format and you do not want to parse it using JSON, then you can do the following.

do your own error checking and sanity check. (handle the presence/absence of spacing, search key not found, etc)

 public static void main(String []args){ 


    String s1 = " \"{\"measurements\": [\"528391^MDC_DEV_SPEC_PROF^M12343\"], \"metrics\": [\"150021^MDC_PRESS_BLD_NONIN234^MDC\", \"150022^MDC_PRESS_BLD_NONINV_DIA^MDC\", \"150023^MDC_PRESS_BLD_12344^MDC\", \"149546^MDC_PULS_RATE_INV^MDC\"], \"accessoryTypes\": [] }\"";

    String testString = "measurements\": [\"";
    String endString = "\"],";

    System.out.println(s1);


    String result = getValue(s1, testString, endString);

    System.out.println("RESULT = "+result);

 }

 public static String getValue(String s1, String keyString, String endString){

    int from = s1.indexOf(keyString) + keyString.length(); 
    int to = s1.substring(from).indexOf(endString); 
    return s1.substring(from, from+to); 

 }

you result will be like the following

$java -Xmx128M -Xms16M HelloWorld
 "{"measurements": ["528391^MDC_DEV_SPEC_PROF^M12343"], "metrics": ["150021^MDC_PRESS_BLD_NONIN234^MDC", "150022^MDC_PRESS_BLD_NONINV_DIA^MDC", "150023^MDC_PRESS_BLD_12344^MDC", "149546^MDC_PULS_RATE_INV^MDC"], "accessoryTypes": [] }"
RESULT = 528391^MDC_DEV_SPEC_PROF^M12343
Angel Koh
  • 12,479
  • 7
  • 64
  • 91
1

You can parse this String with org.json:

public Example parseJSON(String JSONString){
    Example example = new Example();
    JSONObject jsonObject = new JSONObject(JSONString);
    JSONArray measurementsJSON = jsonObject.getJSONArray("measurements");
    JSONArray metricsJSON = jsonObject.getJSONArray("metrics");
    JSONArray accessoryTypesJSON = jsonObject.getJSONArray("accessoryTypes");

    String[] measurements = new String[measurementsJSON.length()];
    for(int i = 0; i < measurementsJSON.length; i++){
        measurements[i] = measurementsJSON.getString(i);
    }
    String[] metrics = new String[metricsJSON.length()];
    for(int i = 0; i < metricsJSON.length; i++){
        metrics[i] = metricsJSON.getString(i);
    }
    String[] accessoryTypes = new String[accessoryTypesJSON.length()];
    for(int i = 0; i < accessoryTypesJSON.length; i++){
        accessoryTypes[i] = accessoryTypesJSON.getString(i);
    }

    example.setMeasurements(Arrays.asList(measurements));
    example.setMetrics(Arrays.asList(metrics));
    example.setAccessoryTypes(Arrays.asList(accessoryTypes));

    return example;
}

Example class:

public class Example {

private List<String> measurements = null;
private List<String> metrics = null;
private List<String> accessoryTypes = null;

public List<String> getMeasurements() {
    return measurements;
}

public void setMeasurements(List<String> measurements) {
    this.measurements = measurements;
}

public List<String> getMetrics() {
    return metrics;
}

public void setMetrics(List<String> metrics) {
    this.metrics = metrics;
}

public List<String> getAccessoryTypes() {
    return accessoryTypes;
}

public void setAccessoryTypes(List<String> accessoryTypes) {
    this.accessoryTypes = accessoryTypes;
}

}

Example class was generated with jsonschema2pojo. To get measurements call:

Example example = parseJSON(input);
    example.getMeasurements();

This was made with core Java. However, I really suggest you to use some JSON 2 Object library.

Vlad Zaichyk
  • 125
  • 9
0

First : create a class Matching with your JSON (Homework you must at least do and post with your initial request)

Second : decide is stocked your JSON (file…) Because your string isn't a well built String

Third : choose a Library (GSON or core java…) to extrac JSON 2 Object

kevin ternet
  • 4,514
  • 2
  • 19
  • 27
0

the clean way should be parsing it as a json, but if this is too over-engineered, then you could just write a matcher with a regex, something like this:

public String parseMeasurements(String s) {
    final Matcher matcher  = Pattern.compile("\"measurements\": \[\"(.*?)\"\]").matcher(s);
    return matcher.group(1);
}

please check if the the brackets are escaped properly and if the regex is not "greedy" but "lazy" in order to stop matching a the first closing bracket. the "?" in "(.*?)" should make it lazy

Oliver
  • 86
  • 1
  • 8
0

You can pass your string to the class and can get a DTO Object, from there

import com.google.gson.GsonBuilder;

GsonBuilder gsonBuilder = new GsonBuilder();
        MeasurementsDTO measurementsDTO = gsonBuilder.create().fromJson(s1,
                MeasurementsDTO.class);
Shiva kumar
  • 673
  • 8
  • 23