将 js.executeAsyncScript 输出转换为 Map<String,String>
casting of js.executeAsyncScript output to Map<String,String>
我想将以下脚本的输出转换为 Map 但它显示“java.lang.String 无法转换为 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);
我想将以下脚本的输出转换为 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);