zoukankan      html  css  js  c++  java
  • json如何转换为bson

    function TMyMongoDbClass.JsonToBson(code:string;obj: tjsonvalue): tbson;
    var
    b:tbson;
    jv:tjsonvalue;
    buffer:tbsonbuffer;
    function BL_json(curbuf:tbsonbuffer;pname:string;tobj:tjsonvalue):tbson;
    var
    i:integer;
    p:tjsonpair;
    pn,s1,s2:string;
    ja:tjsonarray;
    o:tjsonobject;
    tb:tbson;
    begin
    //{name:001,arr:[{obj:1},{obj:2}]}
    if tobj is tjsonnumber then
    begin
    s2:=tobj.ToString;
    if s2.StartsWith('"') then
    delete(s2,1,1);
    if s2.EndsWith('"') then
    delete(s2,length(s2),1);

    curbuf.append(pname,strtofloat(s2));
    exit;
    end;
    if tobj is tjsonstring then
    begin
    s2:=tobj.ToString;
    if s2.StartsWith('"') then
    delete(s2,1,1);
    if s2.EndsWith('"') then

    delete(s2,length(s2),1);
    curbuf.append(pname,s2);
    exit;
    end;
    if tobj is tjsontrue then
    begin
    curbuf.append(pname,true);
    exit;
    end;
    if tobj is tjsonfalse then
    begin
    curbuf.append(pname,false);
    exit;
    end;


    if tobj is tjsonarray then
    begin
    ja:=tobj as tjsonarray;
    curbuf.startArray(pname);
    for i := 0 to ja.count-1 do
    begin
    //tb:=bl_json(tbuf,ja.Items[i]);
    BL_Json(curbuf,'',ja.Items[i]);
    end;
    curbuf.finishObject;
    end;
    if tobj is tjsonobject then
    begin
    //{name:001,arr:[{obj:1},{obj:2}]}
    buffer.startObject(pname);
    o:=tobj as tjsonobject;
    for i := 0 to o.Count-1 do
    begin
    s1:=o.Pairs[i].JsonString.ToString;
    delete(s1,1,1);
    delete(s1,length(s1),1);

    if o.Pairs[i].JsonValue is tjsonnumber then
    begin
    s2:=o.Pairs[i].jsonValue.ToString;
    if s2.StartsWith('"') then
    delete(s2,1,1);
    if s2.EndsWith('"') then
    delete(s2,length(s2),1);
    curbuf.append(s1,strtofloat(s2));
    continue;
    end;
    if o.Pairs[i].JsonValue is tjsonstring then
    begin
    s2:=o.Pairs[i].JsonValue.ToString;
    if s2.StartsWith('"') then
    delete(s2,1,1);
    if s2.EndsWith('"') then

    delete(s2,length(s2),1);
    curbuf.append(s1,s2);
    continue;
    end;
    if o.Pairs[i].JsonValue is tjsontrue then
    begin
    curbuf.append(s1,true);
    continue;
    end;
    if o.Pairs[i].JsonValue is tjsonfalse then
    begin
    curbuf.append(s1,false);
    continue;
    end;


    BL_Json(curbuf,s1,o.Pairs[i].JsonValue);
    end;
    curbuf.finishObject;
    end;

    end;

    begin
    //解析json
    buffer:=tbsonbuffer.Create;

    BL_json(buffer,'code',obj);
    b:=buffer.finish;
    exit(b);
    end;

  • 相关阅读:
    UseControls ————用户自定义控件的textbox的传值问题
    listview 的用法 (asp.net3.5新增控件)
    DataList 中 HyperLink传递时多个参数怎么绑定
    fileupload 上传文件函数 把图片名用日期时间命名
    repeat 的两个事件ItemDataBound和ItemCommand
    dataset.xsd的定义(vs2008)
    asp.net 的框架问题 使多个页面在同一个页面中显示
    第八章 支持与自我了悟
    项目管理心得:一个项目经理的个人体会、经验总结(转)
    细说委托(转)
  • 原文地址:https://www.cnblogs.com/HuiLove/p/4513862.html
Copyright © 2011-2022 走看看