如何在json中获取字符串值,并从下面的请求获取消息值


{
  "Header": {
    "AppId": "appiddfdsf324",
    "RecId": "fdsfrecid79878_879898_8797",
    "SecureRefId": "fsdf5679567fsd_6789678",
    "Type": "Other",
    "Ver": "9.0.0",
    "StartTS": "2016-09-26:07:48.798798-04:00"
  }, 
  "Application": {
    "APP_OS": "Windows",
    "APP_Runtime": ".Net67986",
    "APP_AppName": "MPS",
    "APP_AppVersion": "9.0.0.0",
    "Host": "fsdhajkfh657895fsdajf",
    "Channel": "N/A",
    "APP_ReqId": "2f3d7987987-78987-987987-897-da"
  },
  "Service": {
    "Key": "modification process",
    "CallType": "HGDL",
    "Operation": "processrequest",
    "Port": "n/a"
  },
  "Results": {
    "Elapsed": 0,
    "Message": "Message Succesfully Deleted",
    "TraceLevel": "Information"
  },
  "Security": {
    "Vendor": "abfsdf"
  },
  "Extended_Fields": {
    "CustomerId": "4564987987",
    "MessageId": "768789fsdafasdf987987987fasdf",
    "TimeElapsed": "1272.8171"
  }
}

在上面的字符串值,我们是从网站的结果值捕获,我们将得到一个字符串格式使用selenium webdriver。这我需要转换和读取"消息"的值

注意:我试过下面的代码

JsonElement jelement = new JsonParser().parse((String) elementText);
JsonObject  jobject = jelement.getAsJsonObject();
jobject.getAsJsonObject("Results");

以上将提供result json的完整结果值,但我需要获取与"Message"一起出现的值

第一行下面通过传递字符串[JSON格式的,它应该是JSON格式的,否则它将通过一个异常]创建JSONObject

在第二行,你现在有一个JSON对象,你可以从中获取任何元素,

e.g to fetch the value for Message, which is an element of "Results" object which is of JSON type, 

可以使用。根据你取的是什么使用get

e.g getString -> for getting String, 
    getInt->for getting Integer, 
    JSONObject-> for getting an JSONObject 
    getJSONArray-> for getting a JSONArray

JSONObject jsonObj=new JSONObject(pass your String) //This converts in to JSON Object
jsonObj.getJSONOObject("Results").getString("Message"); //As result internally itself is a JSON Object

相关内容

  • 没有找到相关文章

最新更新