ts string 转map<string, object>
时间: 2024-09-06 17:07:37 浏览: 103
在TypeScript中,要将`string`数组转换为`Map<string, object>`类型,你需要首先创建一个映射(Map),然后遍历字符串数组并将每个元素作为键值对添加到Map中。假设我们有一个`strings`数组,你可以这样做:
```typescript
let strings: string[] = ["key1", "key2", "key3"];
let mapStringToObject: Map<string, object> = new Map();
strings.forEach((str) => {
// 如果你想给每个value一个默认的对象,例如空对象 {}
mapStringToObject.set(str, {});
// 或者直接赋予某个值,比如:
// mapStringToObject.set(str, { valueForKey: str });
});
// 现在mapStringToObject是一个Map,键是字符串,值是对应的object
```
如果你想要自动根据字符串生成对象,可能需要一个函数来构建这些对象。例如:
```typescript
function createObjectFromKey(key: string): object {
return { [key]: key };
}
let mapStringToObject: Map<string, object> = new Map(strings.map(createObjectFromKey));
```
相关问题
for (Map<String, Object> map : dataList) { String lx = map.get("lx").toString(); double syts = 0.0; for (Map<String, Object> maps2 : dataList) { int i = DateUtil.compareDate(map.get("cjsj").toString(),maps2.get("cjsj").toString()); double qjts =0.0; if (StringUtils.equals(lx, "1") && i>=0) { String qjkssj = map.get("qjkssj").toString(); String qjjssj = map.get("qjjssj").toString(); String startTime = map.get("start_date").toString(); String endTime = map.get("end_date").toString(); StringBuffer stringBuffer = new StringBuffer(); StringBuffer append = stringBuffer.append(qjkssj).append(" ").append(startTime); StringBuffer stringBuffer1 = new StringBuffer(); StringBuffer append1 = stringBuffer1.append(qjjssj).append(" ").append(endTime); map.put("startTime", append); map.put("endTime", append1); map.put("jsts",map.get("qjts")); map.put("zjts",0); // qjts = Double.parseDouble("-"+(String) maps2.get("qjts")); qjts = Double.parseDouble("-" + maps2.get("qjts")); } else { map.put("jsts",0); map.put("zjts",map.get("qjts")); qjts = Double.parseDouble((String) maps2.get("qjts")); } syts +=qjts; } map.put("syts",syts);为什么syts 计算的不是负数
根据代码中的逻辑,syts是一个累加值,它在每次循环中都会加上qjts。在if语句块中,当lx等于1且i大于等于0时,qjts等于负数,因为代码中有"-"+,这是把负数转换成字符串的写法,所以syts会累加上负数。在else语句块中,qjts等于正数,因此syts会累加上正数。因此,syts计算的结果取决于qjts的正负性,而不是固定的正数或负数。
当前已知URL地址为“www.test.com”。 token生成方式为该代码段: private String buildToken( String currentTeamMemberName,String userId) { Map<String, String> kv = new LinkedHashMap<>(); kv.put("userId", userId); kv.put("currentTeamMemberName",currentTeamMemberName); kv.put("salt", "salt"); String signature = Sha1Crypto.encode(JsonHelper.getInstance().write(kv)); kv.remove("salt"); kv.put("signature", signature); kv.put("ts", System.currentTimeMillis()+""); String offset = Configuration.getInstance().getProperty("indicatorPlatformOffset"); if(offset == null || offset.trim().length() == 0){ offset = 1000 * 60 * 5 + ""; } kv.put("offset", offset); String token = JsonHelper.getInstance().write(kv);//Map转JSON String base64Token = Base64Codec.encode(token);//base64编码 return base64Token; } 需要编写一个接口,已知参数为userId(单点账户),currentTeamMemberName(账号所属团队的成员名称),offset(偏移量)值为300000; 接口将生成的token添加到cookie中,请求已知的URL地址,供其进行校验。 其地址的校验方法为: private boolean checkRefererTokenStr(String tokenStr) { try{ Map token = JsonHelper.getInstance().read(Base64Codec.decode(tokenStr), Map.class); String userId = (String) token.get("userId"); Map<String, String> signChecker = new LinkedHashMap<>(); signChecker.put("userId", userId); signChecker.put(CURRENT_TEAM_MEMBER_NAME, token.get(CURRENT_TEAM_MEMBER_NAME).toString()); signChecker.put("salt", "salt"); String sign = Sha1Crypto.encode(JsonHelper.getInstance().write(signChecker)); return sign.equals(token.get("signature")); }catch (Exception e){ log.error("验证单点集成页面请求referer失败!", e); } return false; } 请编写接口实现该需求
以下是一个简单的Java实现,可以作为一个Servlet或者Spring MVC Controller来使用:
```java
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TokenController {
private static final String CURRENT_TEAM_MEMBER_NAME = "currentTeamMemberName";
@RequestMapping("/generateToken")
public String generateToken(HttpServletRequest request, HttpServletResponse response,
@RequestParam("userId") String userId,
@RequestParam("currentTeamMemberName") String currentTeamMemberName,
@RequestParam(value = "offset", defaultValue = "300000") long offset) {
Map<String, String> kv = new LinkedHashMap<>();
kv.put("userId", userId);
kv.put(CURRENT_TEAM_MEMBER_NAME, currentTeamMemberName);
kv.put("salt", "salt");
String signature = Sha1Crypto.encode(JsonHelper.getInstance().write(kv));
kv.remove("salt");
kv.put("signature", signature);
kv.put("ts", System.currentTimeMillis() + "");
kv.put("offset", offset + "");
String token = JsonHelper.getInstance().write(kv);
String base64Token = Base64Codec.encode(token);
Cookie cookie = new Cookie("token", base64Token);
cookie.setPath("/");
response.addCookie(cookie);
return "Token generated successfully!";
}
@RequestMapping("/validateToken")
public String validateToken(HttpServletRequest request, HttpServletResponse response) {
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("token")) {
String base64Token = cookie.getValue();
String token = Base64Codec.decode(base64Token);
if (checkRefererTokenStr(token)) {
return "Token is valid!";
} else {
return "Token is invalid!";
}
}
}
}
return "Token not found!";
}
private boolean checkRefererTokenStr(String tokenStr) {
try {
Map<String, Object> token = JsonHelper.getInstance().read(Base64Codec.decode(tokenStr), Map.class);
String userId = (String) token.get("userId");
Map<String, String> signChecker = new LinkedHashMap<>();
signChecker.put("userId", userId);
signChecker.put(CURRENT_TEAM_MEMBER_NAME, token.get(CURRENT_TEAM_MEMBER_NAME).toString());
signChecker.put("salt", "salt");
String sign = Sha1Crypto.encode(JsonHelper.getInstance().write(signChecker));
return sign.equals(token.get("signature"));
} catch (Exception e) {
log.error("验证单点集成页面请求referer失败!", e);
}
return false;
}
}
```
这里使用了Spring MVC的注解来定义了两个接口:`/generateToken` 和 `/validateToken`,其中 `/generateToken` 接受三个参数:`userId`、`currentTeamMemberName` 和 `offset`,并生成一个token添加到Cookie中;`/validateToken` 从Cookie中读取token,并验证其有效性。
为了简化代码,这里使用了一些第三方库,你需要在项目中添加这些库的依赖:
- org.springframework:spring-web
- com.fasterxml.jackson.core:jackson-core
- com.fasterxml.jackson.core:jackson-databind
- org.apache.commons:commons-lang3
阅读全文