| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4585 人关注过本帖
标题:有没有将 dbf,转成 Json 格式的好办法?
只看楼主 加入收藏
wcx_cc
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:52
帖 子:352
专家分:1152
注 册:2015-10-2
结帖率:92.86%
收藏
已结贴  问题点数:50 回复次数:8 
有没有将 dbf,转成 Json 格式的好办法?
Json 格式,是一种纯文本。虽然广域网上传输速度快,比起 XML 简单一点,但是,将dbf 内的记录,转成 Json,非常啰嗦,靠暴力组合。各位版主,高手们有没有好的办法?
搜索更多相关主题的帖子: dbf 转成 格式 办法 记录 
2018-01-15 20:36
xinjie
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:19
帖 子:271
专家分:520
注 册:2007-8-11
收藏
得分:25 
内事不决问百度。。。。。。

外事不决。。。。
https://www.
2018-01-16 01:32
xinjie
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:19
帖 子:271
专家分:520
注 册:2007-8-11
收藏
得分:0 
补充:当然,木瓜也有一个DLL可以。。。如果你可以找到。。。
2018-01-16 01:34
wcx_cc
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:52
帖 子:352
专家分:1152
注 册:2015-10-2
收藏
得分:0 
木瓜大师的那个我看了,不是从dbf 转的。稍有点简单,我需要的稍有点复杂,谢谢楼上。我再找找资料看看吧。
2018-01-16 22:30
厨师王德榜
Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18
等 级:贵宾
威 望:199
帖 子:987
专家分:4946
注 册:2013-2-16
收藏
得分:25 
自己写个类解决吧,Json的格式比Xml还简单,通过对DBF格式的分析,不难写出代码。平时太忙,尤其年末,不然就帮你写了。
2018-01-17 10:27
wcx_cc
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:52
帖 子:352
专家分:1152
注 册:2015-10-2
收藏
得分:0 
感谢楼上的版主!能写出一个最好啦。参考参考,帮助学习呀!
2018-01-19 20:52
schtg
Rank: 12Rank: 12Rank: 12
来 自:https://t.me/pump_upp
等 级:贵宾
威 望:67
帖 子:1545
专家分:3003
注 册:2012-2-29
收藏
得分:0 
我电脑中收集的,也不知是哪位大侠的作品,不知对你有用否?
*
* VFP_Json.prg
*
function json_encode(xExpr)
    if vartype(_json)<>'O'
        public _json
        _json = newobject('json')
    endif
return _json.encode(@xExpr)

function json_decode(cJson)
local retval
    if vartype(_json)<>'O'
        public _json
        _json = newobject('json')
    endif
    retval = _json.decode(cJson)
    if not empty(_json.cError)
        return null
    endif
return retval

function json_getErrorMsg()
return _json.cError
   
function recordToJson
local nRecno,i,oObj, cRetVal
    if alias()==''
        return ''
    endif
    oObj = newObject('myObj')
    for i=1 to fcount()
        oObj.set(Field(i),eval(Field(i)))
    next
    cRetVal = json_encode(oObj)
    if not empty(json_getErrorMsg())
        cRetVal = 'ERROR:'+json_getErrorMsg()
    endif
return cRetVal

function tableToJson
local nRecno,i,oObj, cRetVal,nRec
    if alias()==''
        return ''
    endif
    nRecno = recno()
    nRec = 1
    dimension aInfo[1]
    scan        
        oObj = newObject('myObj')
        for i=1 to fcount()
            oObj.set(Field(i),eval(Field(i)))
        next
        dimension aInfo[nRec]
        aInfo[nRec] = oObj
        nRec = nRec+1
    endscan
    goto nRecno
    cRetVal = json_encode(@aInfo)
    if not empty(json_getErrorMsg())
        cRetVal = 'ERROR:'+json_getErrorMsg()
    endif
return cRetVal

define class json as custom
    nPos=0
    nLen=0
    cJson=''
    cError=''

    function encode(xExpr)
    local cTipo
        if type('ALen(xExpr)')=='N'
            cTipo = 'A'
        Else
            cTipo = VarType(xExpr)
        Endif
        
        Do Case
        Case cTipo=='D'
            return '"'+dtos(xExpr)+'"'
        Case cTipo=='N'   
            return Transform(xExpr)
        Case cTipo=='L'   
            return iif(xExpr,'true','false')
        Case cTipo=='X'   
            return 'null'
        Case cTipo=='C'
            xExpr = allt(xExpr)
            xExpr = StrTran(xExpr, '\', '\\' )
            xExpr = StrTran(xExpr, '/', '\/' )
            xExpr = StrTran(xExpr, Chr(9),  '\t' )
            xExpr = StrTran(xExpr, Chr(10), '\n' )
            xExpr = StrTran(xExpr, Chr(13), '\r' )
            xExpr = StrTran(xExpr, '"', '\"' )
            return '"'+xExpr+'"'

        case cTipo=='O'
            local cProp, cJsonValue, cRetVal, aProp[1]
            =AMembers(aProp,xExpr)
            cRetVal = ''
            for each cProp in aProp
                if type('xExpr.'+cProp)=='U' or cProp=='CLASS'
                    loop
                endif
                if type( 'ALen(xExpr.'+cProp+')' ) == 'N'
                    Local i,nTotElem
                    cJsonValue = ''
                    nTotElem = Eval('ALen(xExpr.'+cProp+')')
                    For i=1 to nTotElem
                        cmd = 'cJsonValue=cJsonValue+","+ this.encode( xExpr.'+cProp+'[i])'
                        &cmd.
                    Next
                    cJsonValue = '[' + substr(cJsonValue,2) + ']'
                else
                    cJsonValue = this.encode( evaluate( 'xExpr.'+cProp ) )               
                endif
                if left(cProp,1)=='_'
                    cProp = substr(cProp,2)
                endif
                cRetVal = cRetVal + ',' + '"' + lower(cProp) + '":' + cJsonValue
            next
            return '{' + substr(cRetVal,2) + '}'

        case cTipo=='A'
            local valor, cRetVal
            cRetVal = ''   
            for each valor in xExpr
                cRetVal = cRetVal + ',' +  this.encode( valor )
            next
            return  '[' + substr(cRetVal,2) + ']'
            
        endcase

    return ''

    function decode(cJson)
    local retValue
        cJson = StrTran(cJson,chr(9),'')
        cJson = StrTran(cJson,chr(10),'')
        cJson = StrTran(cJson,chr(13),'')
        cJson = this.fixUnicode(cJson)
        this.nPos  = 1
        this.cJson = cJson
        this.nLen  = len(cJson)
        this.cError = ''
        retValue = this.parsevalue()
        if not empty(this.cError)
            return null
        endif
        if this.getToken()<>null
            this.setError('Junk at the end of JSON input')
            return null
        endif
    return retValue
        
   
    function parseValue()
    local token
        token = this.getToken()
        if token==null
            this.setError('Nothing to parse')
            return null
        endif
        do case
        case token=='"'
            return this.parseString()
        case isdigit(token) or token=='-'
            return this.parseNumber()
        case token=='n'
            return this.expectedKeyword('null',null)
        case token=='f'
            return this.expectedKeyword('false',.f.)
        case token=='t'
            return this.expectedKeyword('true',.t.)
        case token=='{'
            return this.parseObject()
        case token=='['
            return this.parseArray()
        otherwise
            this.setError('Unexpected token')
        endcase
    return
        
   
    function expectedKeyword(cWord,eValue)
        for i=1 to len(cWord)
            cChar = this.getChar()
            if cChar <> substr(cWord,i,1)
                this.setError("Expected keyword '" + cWord + "'")
                return ''
            endif
            this.nPos = this.nPos + 1
        next
    return eValue
   

    function parseObject()
    local retval, cPropName, xValue
        retval = createObject('myObj')
        this.nPos = this.nPos + 1
        if this.getToken()<>'}'
            do while .t.
                cPropName = this.parseString()
                if not empty(this.cError)
                    return null
                endif
                if this.getToken()<>':'
                    this.setError("Expected ':' when parsing object")
                    return null
                endif
                this.nPos = this.nPos + 1
                xValue = this.parseValue()
                if not empty(this.cError)
                    return null
                endif               
                retval.set(cPropName, xValue)
                if this.getToken()<>','
                    exit
                endif
                this.nPos = this.nPos + 1
            enddo
        endif
        if this.getToken()<>'}'
            this.setError("Expected '}' at the end of object")
            return null
        endif
        this.nPos = this.nPos + 1
    return retval


    function parseArray()
    local retVal, xValue
        retval = createObject('MyArray')
        this.nPos = this.nPos + 1
        if this.getToken() <> ']'
            do while .t.
                xValue = this.parseValue()
                if not empty(this.cError)
                    return null
                endif
                retval.add( xValue )
                if this.getToken()<>','
                    exit
                endif
                this.nPos = this.nPos + 1
            enddo
            if this.getToken() <> ']'
                this.setError('Expected ] at the end of array')
                return null
            endif
        endif
        this.nPos = this.nPos + 1
    return retval
   

    function parseString()
    local cRetVal, c
        if this.getToken()<>'"'
            this.setError('Expected "')
            return ''
        endif
        this.nPos = this.nPos + 1
        cRetVal = ''
        do while .t.
            c = this.getChar()
            if c==''
                return ''
            endif
            if c == '"'
                this.nPos = this.nPos + 1
                exit
            endif
            if c == '\'
                this.nPos = this.nPos + 1
                if (this.nPos>this.nLen)
                    this.setError('\\ at the end of input')
                    return ''
                endif
                c = this.getChar()
                if c==''
                    return ''
                endif
                do case
                case c=='"'
                    c='"'
                case c=='\'
                    c='\'
                case c=='/'
                    c='/'
                case c=='b'
                    c=chr(8)
                case c=='t'
                    c=chr(9)
                case c=='n'
                    c=chr(10)
                case c=='f'
                    c=chr(12)
                case c=='r'
                    c=chr(13)
                otherwise
                    this.setError('Invalid escape sequence in string literal')
                    return ''
                endcase
            endif
            cRetVal = cRetVal + c
            this.nPos = this.nPos + 1
        enddo
    return cRetVal
                    
    function parseNumber()
    local nStartPos,c, isInt, cNumero
        if not ( isdigit(this.getToken()) or this.getToken()=='-')
            this.setError('Expected number literal')
            return 0
        endif
        nStartPos = this.nPos
        c = this.getChar()
        if c == '-'
            c = this.nextChar()
        endif
        if c == '0'
            c = this.nextChar()
        else
            if isdigit(c)
                c = this.nextChar()
                do while isdigit(c)
                    c = this.nextChar()
                enddo
            else
                this.setError('Expected digit when parsing number')
                return 0
            endif
        endif
        
        isInt = .t.
        if c=='.'
            c = this.nextChar()
            if isdigit(c)
                c = this.nextChar()
                isInt = .f.
                do while isDigit(c)
                    c = this.nextChar()
                enddo
            else
                this.setError('Expected digit following dot comma')
                return 0
            endif
        endif
        
        cNumero = substr(this.cJson, nStartPos, this.nPos - nStartPos)
    return val(cNumero)

    function getToken()
    local char1
        do while .t.
            if this.nPos > this.nLen
                return null
            endif
            char1 = substr(this.cJson, this.nPos, 1)
            if char1==' '
                this.nPos = this.nPos + 1
                loop
            endif
            return char1
        enddo
    return
   
    function getChar()
        if this.nPos > this.nLen
            this.setError('Unexpected end of JSON stream')
            return ''
        endif
    return substr(this.cJson, this.nPos, 1)
   
    function nextChar()
        this.nPos = this.nPos + 1
        if this.nPos > this.nLen
            return ''
        endif
    return substr(this.cJson, this.nPos, 1)
   
    function setError(cMsg)
        this.cError= 'ERROR parsing JSON at Position:'+allt(str(this.nPos,6,0))+' '+cMsg
    return
   
    function getError()
    return this.cError

    function fixUnicode(cStr)
        cStr = StrTran(cStr,'\u00e1','?)
        cStr = StrTran(cStr,'\u00e9','?)
        cStr = StrTran(cStr,'\u00ed','?)
        cStr = StrTran(cStr,'\u00f3','?)
        cStr = StrTran(cStr,'\u00fa','?)
        cStr = StrTran(cStr,'\u00c1','?)
        cStr = StrTran(cStr,'\u00c9','?)
        cStr = StrTran(cStr,'\u00cd','?)
        cStr = StrTran(cStr,'\u00d3','?)
        cStr = StrTran(cStr,'\u00da','?)
        cStr = StrTran(cStr,'\u00f1','?)
        cStr = StrTran(cStr,'\u00d1','?)
    return cStr
enddefine

define class myArray as custom
    nSize = 0
    dimension array[1]

    function add(xExpr)
        this.nSize = this.nSize + 1
        dimension this.array[this.nSize]
        this.array[this.nSize] = xExpr
    return

    function get(n)
    return this.array[n]

    function getsize()
    return this.nSize

enddefine

define class myObj as custom
Hidden ;
    ClassLibrary,Comment, ;
    BaseClass,ControlCount, ;
    Controls,Objects,Object,;
    Height,HelpContextID,Left,Name, ;
    Parent,ParentClass,Picture, ;
    Tag,Top,WhatsThisHelpID,Width
        
    function set(cPropName, xValue)
        cPropName = '_'+cPropName
        do case
        case type('ALen(xValue)')=='N'
            local nLen,cmd,i
            this.addProperty(cPropName+'(1)')
            nLen = alen(xValue)
            cmd = 'Dimension This.'+cPropName+ ' [ '+Str(nLen,10,0)+']'
            &cmd.
            for i=1 to nLen
                cmd = 'This.'+cPropName+ ' [ '+Str(i,10,0)+'] = xValue[i]'
                &cmd.
            next
            
        case type('this.'+cPropName)=='U'
            this.addProperty(cPropName,@xValue)
            
        otherwise
            local cmd
            cmd = 'this.'+cPropName+'=xValue'
            &cmd
        endcase
    return
   
    procedure get (cPropName)
        cPropName = '_'+cPropName
        If type('this.'+cPropName)=='U'
            return ''
        Else
            local cmd
            cmd = 'return this.'+cPropName
            &cmd
        endif
    return ''

enddefine


*
*  test_vfp_json.prg
*
set proc to VFP_json additive

create cursor customers (id n(5), name c(50), lastname c(50), phone c(30))
insert into customers values (1,'Ignacio','Gutierrez','(653)534-8800')
insert into customers values (2,'Antonio','Esparza','(81)8347-1411')
insert into customers values (3,'David','Flores','(653)534-2755')

? 'Json Representing for each customer'
select customers
scan
    ? recordToJson()
endscan

?
?
? 'Now json represent of a whole table'
go top
? tableToJson()
收到的鲜花
  • 厨师王德榜2018-01-21 13:46 送鲜花  39朵   附言:好文章
  • wcx_cc2018-01-23 03:09 送鲜花  10朵   附言:感谢这位朋友!受评分限制无法多加,否则给 ...
2018-01-20 19:53
laibinhua
Rank: 2
等 级:论坛游民
威 望:1
帖 子:283
专家分:38
注 册:2010-12-23
收藏
得分:0 
回复 7楼 schtg
你好,请问如何按变量生成JSON呢?比如c#的
            //生成JSON字符串,其实就把我们刚刚写的实体实体赋值
            Root rt = new Root();
            这个是我赋值的com";
            rt.condition="这个是我赋值的condition";
            rt.ischeck="这个是我赋值的ischeck";
            rt.message="这个是我赋值的message";
            rt.state="这个是我赋值的satate";
            rt.status="这个是我赋值的statcs";
            List<DataItem> data  =new List<DataItem>();
            DataItem dt = new DataItem();
            dt.context="这个是我赋值的内容";
            dt.time="这个是我赋值的时间";
            dt.ftime="这个是我赋值的时间";
            data.Add(dt);
            rt.data=data;
            //把我们初始化好的对象传入即可
            string json = JsonConvert.SerializeObject(rt);


[此贴子已经被作者于2018-12-21 10:10编辑过]

2018-12-20 15:09
wcx_cc
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:52
帖 子:352
专家分:1152
注 册:2015-10-2
收藏
得分:0 
过去的回帖一个帖子,再置顶一下。
2020-01-09 03:25
快速回复:有没有将 dbf,转成 Json 格式的好办法?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016934 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved