我想将下面脚本的输出转换为Map<string,string>但是它显示了">java.lang。字符串不能强制转换为java.util.Map"错误。我们如何在地图中投射??
final JavascriptExecutor js = (JavascriptExecutor) driver;
Map<String,String> str = new HashMap<>();
str = (Map<String, String>) js.executeAsyncScript("var myHeaders = new Headers();n" +
"myHeaders.append('client-id', 'LPDP');n"
"let inputEntities = new Map();n" +
"inputEntities.set("Commons$customerId", "\"A2ZLDCQRXMMNLG\"")n" +
"inputEntities.set("Commons$marketPlaceId", "\"A2XZLSVIQ0F4JT\" +
"let entitiesToBeResolved = ["Rewards$APPA$GetAllPromotions$applicablePromotionDetailList"]n" +
"n" +
"const executeInput = {n" +
""inputEntities": Object.fromEntries(inputEntities),n" +
""entitiesToBeResolved": entitiesToBeResolved,n" +
"};n" +
"var obj n" +
"n" +
"fetch("url", {n" +
" method: 'POST',n" +
" headers: myHeaders,n" +
" body: JSON.stringify(executeInput),n" +
"})n" +
" .then(response => response.text())n" +
" .then(arguments[0])n" +
" .then(result => obj = result);n" +
"n" +
" return obj;");
我假设你的回答是Json
格式。可以用Gson
将String
转化为Map
import com.google.gson.Gson;
Map<String, String> str = new HashMap<String, String>();
String response = js.executeAsyncScript("....");
str = new Gson().fromJson(response, HashMap.class);