Search code examples
javajsonmule-esb

How can I pass this String obtained to jsonObject format?


Executing the code I get a String with the following value "idStation=6107AAE80593E4B2&timestamp=1558524847&pm1=0.800&pm2_5=1.510&pm10=2.650&temperature=22.380&humidity=40.379&pressure=93926.656&luminosity=131&coC=0.440923810000&no2C=0.000000000000&o3C=8.210327100000&batteryLevel=27&batteryCurrent=0&baterryVolts=3.63"

My goal is to convert that String into JsonObject format where each value is separated, that is, idstation = 6107AAE80593E4B2, etc., and to be able to continue treating the data later

the idea is to take for example the value of no2 and save it in a variable of type (Map String, Object)

eventPayload.put ("no2", String.valueOf (no2));

the value of the string is coded in the variable "sinCifrar"

I tried the following code, but I have problems:

'String jsonString = sinCifrar;
JSONObject jsonk = new JSONObject(jsonString);
            no2 = (((jsonk.getDouble("pressure")/101325.0)*(jsonk.getDouble("no2C")/1000)*46.0055)/(0.082*(jsonk.getDouble("temperature")+273.15)))*1000000.0;
            co = (((jsonk.getDouble("pressure")/101325.0)*(jsonk.getDouble("coC")/1000)*28.01)/(0.082*(jsonk.getDouble("temperature")+273.15)))*1000000.0;
            o3 = (((jsonk.getDouble("pressure")/101325.0)*(jsonk.getDouble("o3C")/1000)*48.0)/(0.082*(jsonk.getDouble("temperature")+273.15)))*1000000.0;'

I get the following error:

org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]

Since it is not a string created from the beginning, but it is obtained after executing several methods, I can not leave it with the requested format, any idea?


Solution

  • The string is not a valid JSON but what you want , can be acheived using simple java code as below

    public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
        String test = "idStation=6107AAE80593E4B2&timestamp=1558524847&pm1=0.800&pm2_5=1.510&pm10=2.650&temperature=22.380"
                + "&humidity=40.379&pressure=93926.656&luminosity=131&coC=0.440923810000&no2C=0.000000000000&o3C=8.210327100000&"
                + "batteryLevel=27&batteryCurrent=0&baterryVolts=3.63";
        String[] result = test.split("&");
        Map<String, String> map = new HashMap<String, String>();
        for (String string : result) {
            String[] str = string.split("=");
            map.put(str[0], str[1]);
        }
        double pressure = Double.valueOf(map.get("pressure"));
        double no2C = Double.valueOf(map.get("no2C"));
        double tempreture = Double.valueOf(map.get("temperature"));
        double o3C = Double.valueOf(map.get("o3C"));
        double cOC = Double.valueOf(map.get("coC"));
        System.out.println("Pressure:: "+pressure+" , no2c :: "+no2C+", tempreture:: "+tempreture+" ,o3C:: "+o3C+" ,coC:: "+cOC);
    }
    

    output

    Pressure:: 93926.656 , no2c :: 0.0, tempreture:: 22.38 ,o3C:: 8.2103271 ,coC:: 0.44092381
    

    From the map you can get any Key value you want.