0

我的代码是

ClientRequest request1 = new ClientRequest(url);
request1.accept("application/json");
ClientResponse<String> response1 = request1.get(String.class);
System.out.println(response1);

BufferedReader br = new BufferedReader(new InputStreamReader(
    new ByteArrayInputStream(response1.getEntity().getBytes())));

String output=null;
while ((output = br.readLine()) != null) {
    System.out.println(output);
}

我需要从输出中提取对象 ID,然后从中获取时间戳。

输出:

{ 
  "_id" : ObjectId("5252e015b11ce2799f84334d"), 
  "ConductivityChangeAlarm" : "1", 
  "LevelAlarm" : "1", 
  "WaterTankLevel" : "100cm", 
  "DeviceIndicator" : "1", 
  "HighPresuurePump" : "1", 
  "LowPressureAlarm" : "0", 
  "PremeateFlowRateDisplay" : "5gpm", 
  "CartridgeFilterPressureDropIncrease" : "0", 
  "RawWaterFlowRate" : "20gpm", 
  "TotaliseronHMI" : "1", 
  "FeedConductivityDisplay" : "500μS", 
  "RecirculationFlowRate" : "150gpd", 
  "HighPressureDisplay" : "32psi" 
}
4

1 回答 1

0

您需要做的就是从 String创建import org.json.JSONObject( ) 并对其进行操作。JSONObject

String SampleJSON = "{ \"_id\" : ObjectId(\"5252e015b11ce2799f84334d\")}";
JSONObject resultObject = new JSONObject(SampleJSON);
String value_ID = resultObject.get("_id").toString();
/* Manipulate as you want. Like remove () and all */
System.out.println(value_ID);
于 2013-10-09T06:04:25.547 回答