1. ? ?
  2. PowerBuilder
  3. Friday, 3 February 2023 01:37 AM UTC

我有下面这字符串,怎样转成 json 格式字符串?

{"touser":"oJdQp6FiYmoPF0Mk6ef-aa","template_id":"RtP5_NgqozjErem7Ju-rgUfKv3sG1TH74EJeeee","data":{"first":{"value":"物资(项目)采购比价提醒"},"keyword1":{"value":"2022-11-22"},"keyword2":{"value":"电动润滑泵采购"},"keyword3":{"value":"截止时间:2022-11-25"},"keyword4":{"value":"余祖赢"},"keyword5":{"value":"比价单单号:1-20221122-0003"},"remark":{"value":"请对应项目供应商尽快提供比价单"}}}

 

反过来,如果有json格式字符串,我怎样转为上面字符串? 谢谢

Peter Pang @Appeon Accepted Answer Pending Moderation
  1. Friday, 3 February 2023 08:50 AM UTC
  2. PowerBuilder
  3. # 1

 

请参考下面示例:

string ls_json2
JsonGenerator lnv_JsonGenerator
Long ll_RootObject, ll_RootObject2, ll_RootObject3
lnv_JsonGenerator = Create JsonGenerator

//{"touser":"oJdQp6FiYmoPF0Mk6ef-aa","template_id":"RtP5_NgqozjErem7Ju-rgUfKv3sG1TH74EJeeee","data":{"first":{"value":"物资(项目)采购比价提醒"},"keyword1":{"value":"2022-11-22"}}}
ll_RootObject = lnv_JsonGenerator.CreateJsonObject()
if ll_RootObject <> -1 then
	lnv_JsonGenerator.additemstring( ll_RootObject,"touser", "oJdQp6FiYmoPF0Mk6ef-aa")
	lnv_JsonGenerator.additemstring( ll_RootObject,"template_id", "RtP5_NgqozjErem7Ju-rgUfKv3sG1TH74EJeeee")
	
	ll_RootObject2 = lnv_JsonGenerator.AddItemObject ( ll_RootObject,"data")
	
	ll_RootObject3 = lnv_JsonGenerator.AddItemObject ( ll_RootObject2,"first")  
	lnv_JsonGenerator.additemstring( ll_RootObject3,"value", "物资(项目)采购比价提醒")
	
	ll_RootObject3 = lnv_JsonGenerator.AddItemObject ( ll_RootObject2,"keyword1")  
	lnv_JsonGenerator.additemstring( ll_RootObject3,"value", "2022-11-22")
	//...
  
end if

ls_json2 = lnv_JsonGenerator.GetJsonString( )
MessageBox("", ls_json2)
Comment
There are no comments made yet.
? ? Accepted Answer Pending Moderation
  1. Friday, 3 February 2023 08:11 AM UTC
  2. PowerBuilder
  3. # 2

 

怎样处理,谢谢解答

Comment
There are no comments made yet.
Peter Pang @Appeon Accepted Answer Pending Moderation
  1. Friday, 3 February 2023 05:37 AM UTC
  2. PowerBuilder
  3. # 3

你好,

这就是标准的json字符串,你意思是把中文转为\uxxxx吗?

用JsonGenerator重新生成Json串:

string ls_json, ls_Path, ls_json2
Long 		ll_Return, ll_item
JsonGenerator lnv_JsonGenerator
lnv_JsonGenerator = Create JsonGenerator


ls_json = '{"touser":"oJdQp6FiYmoPF0Mk6ef-aa","template_id":"RtP5_NgqozjErem7Ju-rgUfKv3sG1TH74EJeeee","data":{"first":{"value":"物资(项目)采购比价提醒"},"keyword1":{"value":"2022-11-22"},"keyword2":{"value":"电动润滑泵采购"},"keyword3":{"value":"截止时间:2022-11-25"},"keyword4":{"value":"余祖赢"},"keyword5":{"value":"比价单单号:1-20221122-0003"},"remark":{"value":"请对应项目供应商尽快提供比价单"}}}'
ll_Return = lnv_JsonGenerator.importstring( ls_json)

ls_Path = "/touser"
ll_item = lnv_JsonGenerator.GetItemBypath(ls_Path)
MessageBox("", ll_item)

ls_json2 = lnv_JsonGenerator.GetJsonString( )
MessageBox("", ls_json2)

 

用JsonParser解析json串:

JsonParser lnv_JsonParser
String ls_Json, ls_name
Long ll_RootObject, ll_item
lnv_JsonParser = Create JsonParser

ls_Json = '{"touser":"oJdQp6FiYmoPF0Mk6ef-aa","template_id":"RtP5_NgqozjErem7Ju-rgUfKv3sG1TH74EJeeee","data":{"first":{"value":"物资(项目)采购比价提醒"},"keyword1":{"value":"2022-11-22"},"keyword2":{"value":"电动润滑泵采购"},"keyword3":{"value":"截止时间:2022-11-25"},"keyword4":{"value":"余祖赢"},"keyword5":{"value":"比价单单号:1-20221122-0003"},"remark":{"value":"请对应项目供应商尽快提供比价单"}}}'

lnv_JsonParser.LoadString(ls_Json)
ll_RootObject = lnv_JsonParser.GetRootItem()
ll_item = lnv_JsonParser.GetChildItem(ll_RootObject, 2)
ls_name = lnv_JsonParser.GetItemString(ll_item)

MessageBox("",ls_name )

ls_name = lnv_JsonParser.GetItemString("/data/first/value")

MessageBox("",ls_name )
Comment
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.