如何在javascript中将json objectId转换为其字符串表示形式
我想将RESTMongoDBAPI返回的bson ObjectId的json表示转换为字符串 from:{“inc”:1365419770,“machine”:-856505582,“timeSecond”:1375343587,“time”:1375343587000,“new”:false} 收件人:51fa13e3ccf2c3125162a6fa如何在javascript中将json objectId转换为其字符串表示形式,javascript,json,mongodb,bson,objectid,Javascript,Json,Mongodb,Bson,Objectid,我想将RESTMongoDBAPI返回的bson ObjectId的json表示转换为字符串 from:{“inc”:1365419770,“machine”:-856505582,“timeSecond”:1375343587,“time”:1375343587000,“new”:false} 收件人:51fa13e3ccf2c3125162a6fa 在客户端,它将使用路径参数调用其他API。我刚刚开发了它,以防其他人正在寻找相同的功能 var ObjectIdStr = function (
在客户端,它将使用路径参数调用其他API。我刚刚开发了它,以防其他人正在寻找相同的功能
var ObjectIdStr = function (hexstr) {
this.timestamp ;
this.machine ;
this.increment ;
if (this.__proto__.constructor !== ObjectIdStr) {
return new ObjectIdStr(hexstr);
}
var isValid = function( s ){
if ( s == null )
return false;
len = s.length;
if ( len != 24 )
return false;
for ( i=0; i<len; i++ ){
c = s.charAt(i);
if ( c >= '0' && c <= '9' )
continue;
if ( c >= 'a' && c <= 'f' )
continue;
if ( c >= 'A' && c <= 'F' )
continue;
return false;
}
return true;
}
var fromHex = function(hex){
hex = parseInt(hex, 16);
if (hex > 0x80000000) {
hex = hex - 0xFFFFFFFF - 1;
}
return hex;
}
if ( ! isValid( hexstr ) )
throw "invalid ObjectId [" + s + "]" ;
this.timestamp = fromHex(hexstr.substring(0,8));
this.machine = fromHex(hexstr.substring(8,16));
this.increment = parseInt( hexstr.substring(16,24) , 16);
}
var ObjectId = function (json) {
this.timestamp = json.timeSecond;
this.machine = json.machine;
this.increment = json.inc;
if (this.__proto__.constructor !== ObjectId) {
return new ObjectId(json);
}
var hex = function(number){
if (number < 0) {
number = 0xFFFFFFFF + number + 1;
}
return number.toString(16).toLowerCase();
}
this.toString = function () {
var timestamp = hex(this.timestamp);
var machine = hex(this.machine);
var increment = hex(this.increment);
return '00000000'.substr(0, 6 - timestamp.length) + timestamp +
'00000000'.substr(0, 6 - machine.length) + machine +
'00000000'.substr(0, 6 - increment.length) + increment ;
};
};
function testme(){
var objJson = {"inc":1365419770,"machine":-856505582,"timeSecond":1375343587,"time":1375343587000,"new":false};
$("#ObjIdStr").html(ObjectId(objJson).toString());
obj = ObjectIdStr("51fa13e3ccf2c3125162a6fa")
$("#out").html( obj.increment + " " + obj.machine + " " + obj.timestamp)
}
var objectistr=函数(hexstr){
这个时间戳;
这台机器;
这是增量;
if(this.uu proto_uu.constructor!==ObjectIdStr){
返回新objectdstr(hexstr);
}
var isValid=函数{
如果(s==null)
返回false;
len=s.长度;
如果(len!=24)
返回false;
对于(i=0;i='0'&&c='a'&&c='a'&&c 0x8000000){
十六进制=十六进制-0xFFFFFF-1;
}
返回十六进制;
}
如果(!isValid(hextr))
抛出“无效的ObjectId[“+s+”]”;
this.timestamp=fromHex(hexstr.substring(0,8));
this.machine=fromHex(hextr.substring(8,16));
this.increment=parseInt(hexstr.substring(16,24),16);
}
var ObjectId=函数(json){
this.timestamp=json.timeSecond;
this.machine=json.machine;
this.increment=json.inc;
if(this.\u proto\u.constructor!==ObjectId){
返回新的ObjectId(json);
}
var hex=函数(数字){
如果(数字<0){
数字=0xFFFFFF+数字+1;
}
返回编号.toString(16).toLowerCase();
}
this.toString=函数(){
var timestamp=hex(这个.timestamp);
var机器=十六进制(此机器);
var增量=十六进制(此增量);
返回'00000000'。子字符串(0,6-timestamp.length)+timestamp+
“00000000”.substr(0,6-机器长度)+机器+
“00000000.”子字符串(0,6-增量长度)+增量;
};
};
函数testme(){
var objJson={“inc”:1365419770,“machine”:-856505582,“timeSecond”:1375343587,“time”:1375343587000,“new”:false};
$(“#ObjIdStr”).html(ObjectId(objJson.toString());
obj=ObjectDSTR(“51fa13e3ccf2c3125162a6fa”)
$(“#out”).html(obj.increment+“”+obj.machine+“”+obj.timestamp)
}
您正在使用哪种RESTAPI以这种方式返回ObjectID?内置(不推荐用于生产)API没有:我已经创建了一个基于集合的CRUD包装器,使用一些语法糖来休息。我可以执行以下操作:@Path(“/tag”)公共类TagRest扩展服务。。。并为Tag类获取完整的CRUD