一、我們有一段JSON數據如下:
{ "五班": [ { "姓名": "張三", "成績": 75.5 }, { "姓名": "李四", "成績": 21.7 } ] }
二、使用Delphi代碼讀取,代碼如下:
uses System.Types, System.JSON, System.JSON.Types, System.JSON.Writers, System.JSON.Builders; procedure TForm1.Button4Click(Sender: TObject); var I: Integer; m_JsonStr: string; m_SubArray: TJSONArray; m_JsonObject: TJSONObject; m_SubJsonObj: TJSONObject; begin // 讀取JSON文件 m_JsonStr := Trim(Memo1.Text); m_JsonObject := TJSONObject.ParseJSONValue(TEncoding.UTF8.GetBytes(m_JsonStr), 0) as TJSONObject; // 取最外層 for I := 0 to m_JsonObject.count - 1 do begin Memo2.Lines.Add(m_JsonObject.Get(I).JsonString.toString + ' = ' + m_JsonObject.Get(I).JsonValue.ToString); end; // 取內層 m_SubArray := m_JsonObject.getValue('五班') as TJSONArray; for I := 0 to m_SubArray.size - 1 do begin m_SubJsonObj := m_SubArray.Get(I) as TJSONObject; Memo2.Lines.Add(Format('標簽:%s = %s', [m_SubJsonObj.Get(0).JsonString.ToString, m_SubJsonObj.Get(0).JsonValue.ToString])); Memo2.Lines.Add(Format('標簽:%s = %s', [m_SubJsonObj.Get(1).JsonString.ToString, m_SubJsonObj.Get(1).JsonValue.ToString])); end; end;
三、顯示結果如下:
不忘初心,如果您認為這篇文章有價值,認同作者的付出,可以微信二維碼打賞任意金額給作者(微信號:382477247)哦,謝謝。