org.apache.jorphan.util.JMeterException: Error invoking bsh method: eva

來源:互聯網
上載者:User

標籤:false   []   intvalue   格式   instance   oca   parse   cas   span   

使用fastJson來做json解析,出現報錯。

Assertion failure message: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONArray; i . . . ‘‘ : Typed variable declaration : Method Invocation jsonObject.getJSONObject

原因如下:

要解析的json體是這樣的:

{"code":0,"msg":"ok","data":{"open_id":"2c2d714c1b577332be3eac9784f98d1d"}}

 

代碼如下:

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSON;

String resp=new String(ResponseData);
JSONObject jsonObject=JSON.parseObject(resp);
int code=jsonObject.getIntValue("code");
JSONObject data=jsonObject.getJSONObject("data");

String msg = jsonObject.getString("msg");

if((code==0)&&(msg.equals("ok"))&&(data.toString()!="")){
Failure=false;
}else if (code!=0){
Failure=true;
FailureMessage="code!=0";
}else if(!msg.equals("ok")){
Failure=true;
FailureMessage="msg!=ok";
}else{
Failure=true;
FailureMessage="data返回資料有誤";
}

 

如果期望結果剛好格式和{"code":0,"msg":"ok","data":{"open_id":"2c2d714c1b577332be3eac9784f98d1d"}}一致,就不會報錯。

如果期望結果格式不一致,就會報錯。

比如json格式是這樣的:

{"code":1001,"msg":"\u7b7e\u540d\u5931\u8d25","data":[]}

 

分析之後,得出 json體中,解析data時出現了錯誤。

在eclipse中調試代碼,定位出錯程式碼:

JSONObject data=jsonObject.getJSONObject("data");

原因是,如果data裡面沒有[{xxxxxxx}],那data是個JSONArray,不是JSONObject;

eclipse會這麼報錯:

 java.lang.ClassCastException: com.alibaba.fastjson.JSONArray cannot be cast to com.alibaba.fastjson.JSONObject

 

修改代碼如下:

如果data是JSONObject對象,就賦值JSONObject,如果data是JSONArray,就賦值JSONArray

 

把錯誤碼行 JSONObject data=jsonObject.getJSONObject("data");

替換為:

Object data = "";
if(jsonObject.containsKey("data")){
Object dataObject=jsonObject.get("data");
if(dataObject instanceof JSONObject){
data= jsonObject.getJSONObject("data");
}else if(dataObject instanceof JSONArray){
data=jsonObject.getJSONArray("data");
}
}else{
Failure=true;
FailureMessage="返回結果中沒有data資料";
}

org.apache.jorphan.util.JMeterException: Error invoking bsh method: eva

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.