Ad

Extracting Strings Out Of A String Response

- 1 answer

I have following String response. Out of which I want to extract MAC ID & other fields like Version, Type, devIP etc.

Response ={ 
"Version" : “1.10", 
"Type" :"'xyzTYPE", 
"MaCID" :"ABCD1F2G3900", 
"devIP" : "'192.168.1.100", 
"Signal": "-66", 
"AreaName" :"'power", 
"SubType" :"wifidev", 
"'BuiIdTime" : "11:50:47", 
"'BuiIdDate": "'Nov 2 2018" 
}

Though I have implemented it in this way.

String macID = result.substring(result.indexOf("MaCID")+11,result.indexOf("devIP")-4);

I want to know if there is another sophisticated manner to do the same.

Ad

Answer

Your response looks like JSON, so for json response in result;

 JSONObject lsubObject= new JSONObject(result);
    String MaCID =lsubObject.getString("MaCID");
Ad
source: stackoverflow.com
Ad