导航:
论坛 -> Web应用开发
斑竹:bodies
作者:
2016/12/25 11:09:54
标题:
delphi json解析刘德华这个名字,如何解析?
浏览:2619
加入我的收藏
楼主:
{"zt": 1, "text": [ {"id": "2",rwzt": "OK ","rwcreat": "刘德华 ","rwsenddt": "2016/12/11"} ], "msg": "查询成功!"} 请教,我要在delphi客户端json解析 刘德华 这个名字,如何解析?谢谢! 备注:zt的json解析没问题,但后面的[]里数组里的解析不会,请指教。
----------------------------------------------
-
作者:
2016/12/25 12:07:50
1楼:
数组的每个成员又是一个对象啊
----------------------------------------------
一个热爱delphi的人,Golang GUI库 。 --我的博客--
作者:
2016/12/25 12:12:46
1楼:
呵呵,BCB 自已写的代码,Delphi 可以参考一下 String __fastcall JSON_Value(TJSONValue * AJSON, String APath) { String Result; String LKey; if (APath.Pos(".")) { LKey = APath.SubString(1, APath.Pos(".")-1); APath.Delete(1, APath.Pos(".")); } else { LKey = APath; APath = ""; } if (LKey.Length()) { TJSONObject * JSON = (TJSONObject *)AJSON; AJSON = JSON->GetValue(LKey); if (AJSON) if (APath.Length()) { if (AJSON->InheritsFrom(__classid(TJSONArray))) { TJSONArray * JArray = (TJSONArray * )AJSON; for (int i = 0; i < JArray->Count; i++) { Result = JSON_Value(JArray->Items[i], APath); if (Result.Length()) break; } } else Result = JSON_Value(AJSON, APath); } else Result = AJSON->Value(); } return Result; } ---------- TJSONValue * JSON = TJSONObject::ParseJSONValue("你的JSON文本"); String S = JSON_Value(JSON, "text.rwcreat"); // S 这时就是 “刘德华”
----------------------------------------------
我84砖家
作者:
2016/12/25 12:13:34
2楼:
以qjson为例: name := qjson.itembyanme('text').items[0].valuebyname('rwcreat','默认值')
----------------------------------------------
-
作者:
2016/12/25 13:33:49
3楼:
有没有直接用delphi举个例子呢? 比如: procedure TForm1.Button3Click(Sender: TObject); var jo:tjsonobject; s1,s2,s3,s4:string; jsonstr:string jr: tjsonarray; begin jo:=nil; jsonstr:='{"zt": 1, "text": [ {"id": "2",rwzt": "OK ","rwcreat": "刘德华 ","rwsenddt": "2016/12/11"} ], "msg": "查询成功!"}'; try jo:=tjsonobject.create; jo:=tjsonobject.parsejsonvalue(tencoding.utf8.getbytes(jsonstr),0) as tjsonobject; jr:=TJSONArray(jo.GetValue('text')); //获取数组里的数据 s3:=jr.ToString...; //下面不知怎么办 showmessage(s3); finally jo.Free; end; end;
----------------------------------------------
-
作者:
yczjs (DelphiFans)
★☆☆☆☆
-
盒子活跃会员
2016/12/25 16:48:19
4楼:
楼主啊,首先你在http://www.bejson.com/ 解析一下看jsonstr是否正确 正确的是:{"zt": "1", "text": [{"id": "2","rwzt": "OK ","rwcreat": "刘德华 ","rwsenddt": "2016/12/11"}], "msg": "查询成功!"}
----------------------------------------------
-
作者:
yczjs (DelphiFans)
★☆☆☆☆
-
盒子活跃会员
2016/12/25 16:57:24
5楼:
var jo:tjsonobject; s1,s2,s3,s4:string; jsonstr:string; jr: tjsonarray; begin jsonstr:='{"zt": "1", "text": [{"id": "2","rwzt": "OK ","rwcreat": "刘德华 ","rwsenddt": "2016/12/11"}], "msg": "查询成功!"}'; try jo:=tjsonobject.parsejsonvalue(tencoding.utf8.getbytes(jsonstr),0) as tjsonobject; jo := (jo.GetValue('text') as TJSONArray).Get(0) as TJSONObject; s3:=jo.GetValue('rwcreat').Value; showmessage(s3); finally jo.Free; end; end;
----------------------------------------------
-
作者:
2016/12/25 18:47:31
6楼:
楼上厉害,绝对!
----------------------------------------------
-
作者:
2016/12/26 11:03:46
7楼:
这种带中文明码的是非标准JSON格式,标准的应该是中文被编码。\uXXXX JSON标准参见www.json.org
----------------------------------------------
武稀松http://www.raysoftware.cn
作者:
2016/12/26 11:08:10
8楼:
楼上的高手,您给的网站说。 unicode 编码的 txt 内 可以使用 明文。 当然也可以使用 \uXXXX。
----------------------------------------------
(C)(P)Flying Wang
作者:
2016/12/26 14:24:38
9楼:
路径应该是:\text[0]\rwcreat
----------------------------------------------
-
作者:
2016/12/26 21:30:54
10楼:
if pos("刘德华",jsonString)>0 then showmessage("呀!刘德华也在里面");
----------------------------------------------
我的大全[5ddq.com],我的主页!
作者:
2016/12/27 10:22:22
11楼:
@wang_80919 可是如果用明文,你用什么编码呢?GB2312,BIG5? JSON标准是只支持Unicode。
----------------------------------------------
武稀松http://www.raysoftware.cn