Connection lost with jsonArray

this is my code import java.util.Iterator;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SimpleMqttCallBack implements MqttCallback {
private final Logger logger = LoggerFactory.getLogger(FirstHandler.class);

@Override
public void connectionLost(Throwable throwable) {
    logger.info("conection lost");
}

@Override
public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
    logger.info("inside messageArrived");
    String a = new String(mqttMessage.getPayload()); //converting the mqtt message ofjson format in a string
    logger.info(a + "\n");

    String sbr = a.substring(1, 7);
    logger.info(sbr + "\n"); //substring of the string,the code is running fine
    
        JSONArray jsonArray = new JSONArray(a);
        //after this line i got connection lost in my log file..may i know the reason

        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject json = jsonArray.getJSONObject(i);

            JSONObject wallet = (JSONObject) json.get("d");
            Iterator<String> keys1 = wallet.keys();
            while (keys1.hasNext()) {
                // ChannelUID channelUID;
                String key1 = keys1.next();
                if (key1.equals("A_Temperature")) {
                    logger.info("inside temper");
                    logger.info(key1 + "\t" + wallet.get(key1));
                    

                }
                if (key1.equals("A_Humidity")) {
                    logger.info("inside humi");

                    logger.info(key1 + "\t" + wallet.get(key1));
                }
                if (key1.equals("A_Pressure")) {
                    logger.info("inside pres");
                    logger.info(key1 + "\t" + wallet.get(key1));
                }

            }
        }
   
}

@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
}

}

WHOLE CODE IS RUNNING FINE IN JAVA, BUT IN OPENHAB LOGS I GOT CONNECTION LOST!!!
IS THERE ANY PROBLEM WITH THE LIBRARY THAT I AM USING “javax.json-1.0.4.jar”
i also add /lib/javax.json-1.0.4.jar in classpath in manifest file…

why it is happening…

Is there a reason you are not using the MQTT binding rather than writing a new MQTT binding? You can publish and subscribe to/for MQTT messages and the JSONPATH transform can parse data out of JSON formatted strings.

Also, if you are writing a new binding, is there any reason why you are not using the OH/ESH standard log4j2 logging service?

i have done JSONPATH transform to parse data out of JSON formatted strings, with the help of rules, and it is running fine, But I want to create a new binding.
Is there a reason you are not using the MQTT binding rather than writing a new MQTT binding?
YES, actually am doing a project in openhab2 for some other client so he wants a binding which shows his data in openhab2 ui via binding that’s why i have to create a binding that parse JSON using MQTT.
why you are not using the OH/ESH standard log4j2 logging service?
i don’t know about this service. will you please tell me more and if i am not wrong then is this service used to see the log files.?

I recommend reviewing

http://docs.openhab.org/developers/

and

http://www.eclipse.org/smarthome/documentation/development/bindings/how-to.html

In particular (in answer to your logging question):

http://www.eclipse.org/smarthome/documentation/development/guidelines.html#c-language-levels-and-libraries

For logging, slf4j (v1.7.2) is used.

So yep, I’m wrong. You are using the right logging library. My bad. :blush:

However, without knowing more about what you are really trying to do, writing a new library that just does MQTT and JSON parsing is not really the right approach. You can show his data in the openHAB UI with the existing MQTT binding and JSONPATH transform and/or Rules. A new binding is way overkill and completely redundant with existing capabilities.

Hello sir,
My problem has solved,
Finally my own developed Binding is receiving the JSON string via MQTT and parse it correctly and updating the channels in paper as well as in basic UI…
actually I got connection lost every time because I am not sending the JSON string in the correct format as it wants. it was my mistake, and carelessness.
Thanks for your support…

Hello young lady,
May we know what was the purpose of the binding you developed and may it be of use for the wider community?
As you were working with open source software you have a moral duty to share your code back with the community.
Thanks