如何在Javascript中将mac地址转换为long?

时间:2017-02-07 04:08:17

标签: javascript java json mac-address

我的Javascript代码:

    function singleCopySave()
    {   
        var sam1="00:A0:C9:14:C8:29";
        var mac=toLrad.split(":").map(Integer.parseInt(_,16)).foldLeft(0L) {case (acc,item) => acc*256+item};

        var JSONObject = {};
        JSONObject["toLrad"]=sam1;
        var jsonData=dojo.toJson(JSONObject);
        dojo.xhrPost({
               url :"/unified/singleCopy", 
               preventCache:true,
               handleAs: "text", 
               postData:jsonData,
               headers: {"Content-Type": "application/json"},
               sync:true,   
               load: function(response, ioArgs)
                {
                retVal = response;
                },
               error: function(errorResponse, ioArgs)
                {

                }

               });
            }

我的Java REST代码如下所示:

@POST
@Path("/singleCopy")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
public String singleApCopy(sampleDTO dto,@Context HttpServletRequest request, @Context HttpServletResponse response) {
    long toLradId = dto.getToLrad();
return "success";
  

它没有达到REST网址。将字符串值转换为long时出错。有人可以帮我解决转换为long的语法问题吗?       或者有没有办法转换DTO.java中的mac地址?

1 个答案:

答案 0 :(得分:3)

试试这个

var mac= parseInt(sam1.split(':').join(''), 16);

var JSONObject = {};
JSONObject["toLrad"]=mac;

你必须发送json属性toLrad,它能够用Java方法转换