文件包含json格式的发票清单,例如:
{“ status”:“确定”,“ statusCode”:200,“ messages”:null,“数据”:[{“ payment_type”:“银行转账”,“ fine”:“ 0.200000”,“ quote_id”:null,“ order_id”:null,“ prepayment_id”:null,“ credited_invoices”:[],“ interested_party_address_id”:279,“ project_id”:875,“ currency”:“ EUR”,“ owner_id”:3,“ date”:“ 2019-03-15”,“ deadline”:“ 2019-03-20”,},{“ payment_type”:“银行转账”,“ fine”:“ 0.30000”,“ quote_id”:null,“ order_id”:null,“ prepayment_id”:null,“ credited_invoices”:[],“ interested_party_address_id”:79,“ project_id”:85,“ currency”:“ EUR”,“ owner_id”:3,“ date”:“ 2019-04-15”,“ deadline”:“ 2019-43-20”,}....更多相同类型的元素]}
如何将其转换为FoxPRo光标?游标应包含payment_type,fine,quote_id等列。
我尝试过
https://archive.codeplex.com/?p=qdfoxjson
和
http://www.sweetpotatosoftware.com/blog/index.php/2008/12/19/visual-foxpro-json-class-update/
但是看起来他们要求json与我的json格式不同。
安德鲁斯
快速为您提出一些建议。如果我有更多类似格式的东西,我会做一些“查找开始分隔符”,“寻找结束分隔符”,“最终数据类型”等游标/表结构,然后遍历每个部分。在这里,我实际上是在演示如何提取每个部分以及如何一次获取一个数据行。
** Example to put into a string called "json", no other context to json
TEXT TO json noshow
{
"status": "OK", "statusCode": 200, "messages": null,
"data": [{1
"payment_type": "banktransfer", "fine": "0.200000", "quote_id": null, "order_id": null, "prepayment_id": null, "credited_invoices": [],
"interested_party_address_id": 279, "project_id": 875, "currency": "EUR", "owner_id": 3, "date": "2019-03-15", "deadline": "2019-03-20",
},
{2
"payment_type": "banktransfer", "fine": "0.30000", "quote_id": null, "order_id": null, "prepayment_id": null, "credited_invoices": [],
"interested_party_address_id": 79, "project_id": 85, "currency": "EUR", "owner_id": 3, "date": "2019-04-15", "deadline": "2019-43-20",
}
.... more same type elements
]
}
ENDTEXT
** Strip the "Status" component ang get just the Data
json = SUBSTR( json, ATC( ["data":], json ))
CREATE CURSOR C_ImportRecs;
( PayType c(20),;
Fine n(10,6),;
QuoteID i,;
OrderID i,;
PrepayID i,;
Invoices c(20),;
IntPartyAddrID i,;
ProjectID i,;
TransCurrency c(3),;
OwnerID i,;
DatePart c(15),;
TransDate d,;
DeadPart c(15),;
Deadline d )
** scatter all fields to an object which will have fields by same name
SCATTER NAME newRec
** start counter to 1
nRow = 1
DO while .t.
** Look within the json string for the "nRow" instance of data starting / ending with { / }
oneData = STREXTRACT( json, "{", "}", nRow )
** if no more entries, get out
IF EMPTY( ALLTRIM( oneData ))
exit
ENDIF
** if memory variables are same name as cursor/table columns, simplifies insert
newRec.PayType = STREXTRACT( oneData, ["payment_type": "], ["] )
newRec.Fine = VAL( STREXTRACT( oneData, ["fine": "], ["] ))
** not including closing quote as ending delimiter since null allowed, looking for the "," after the IDs
newRec.QuoteID = INT( VAL( STREXTRACT( oneData, ["quote_id": ], [,] ) ))
newRec.OrderID = INT( VAL( STREXTRACT( oneData, ["order_id": ], [,] ) ))
newRec.PrepayID = INT( VAL( STREXTRACT( oneData, ["prepayment_id": ], [,] ) ))
newRec.Invoices = STREXTRACT( oneData, ["credited_invoices": ], [,] )
newRec.IntPartyAddrID = INT( VAL( STREXTRACT( oneData, ["interested_party_address_id": ], [,] ) ))
newRec.ProjectID = INT( VAL( STREXTRACT( oneData, ["project_id": ], [,] ) ))
** Currency is reserved word.
newRec.TransCurrency = STREXTRACT( oneData, ["currency": "], ["] )
newRec.OwnerID = INT( VAL( STREXTRACT( oneData, ["owner_id": ], [,] ) ))
** Date is reserved word
newRec.DatePart = ""
newRec.DatePart = STREXTRACT( oneData, ["date": "], ["] )
** Nice for macro substitution. Take incoming date format of ex: "2019-03-15"
** and change to "2019,03,15" This is because the DATE() function expects
** 3 parameters for YEAR, MONTH, DAY
tryDate = STRTRAN( newRec.DatePart, "-", "," )
** Now, by using macro via "&" forces the line below to immediately run as if
** run as if it was DATE( 2019,03,15) and create an actual date value
TRY
newRec.TransDate = DATE( &tryDate )
CATCH
newRec.TransDate = CTOD( "" )
ENDTRY
** same for deadline
newRec.DeadPart = STREXTRACT( oneData, ["deadline": "], ["] )
tryDate = STRTRAN( newRec.DeadPart, "-", "," )
TRY
** your second record has a month 43 which is an invalid month
newRec.Deadline = DATE( &tryDate )
CATCH
newRec.Deadline = CTOD( "" )
ENDTRY
** Since the cursor "C_ImportRecs" is already the active focus,
** we can just insert from memory variables
INSERT INTO C_ImportRecs FROM NAME newRec
** try if any more records
nRow = nRow + 1
ENDDO
BROWSE NORMAL NOCAPTIONS NOWAIT
首先是您的JSON无效。 “截止日期”日期值后不应出现逗号。
第二件事是没有机制可以告诉VFP如何将元素映射到字段。因此,您必须自己做。
为此,我将使用njJson库,特别是nfJsonRead
。
假设您的JSON位于文件'test.json'中,然后您具有nfJsonRead.prg,例如:
Close All
Clear All
Clear
Set Procedure To nfJsonRead additive
* -- Add the other fields as appropriate.
Create Cursor mydata (payment_type c(20), fine n(12, 2))
loJson = nfjsonread(FileToStr("test.json"))
For each loData in loJson.data
Insert into mydata values (loData.payment_type, Val(loData.fine))
EndFor