如何從我的 JSONObject3 物件開始將物理 .json 檔案保存到我的 C:\ 驅動器?
procedure CreateJSON;
var
JSONObject2, JSONObject3: TJSONObject;
JSONValue1, JSONValue2: TJSONValue;
JSONArray: TJSONArray;
JSONString1, JSONString2: TJSONString;
AText, BText: string;
mStringStream: TStringStream;
begin
JSONObject2 := TJSONObject.Create;
JSONObject3 := TJSONObject.Create;
JSONArray := TJSONArray.Create;
try
AText := 'Name';
BText := '"Charles"';
JSONString2 := TJSONString.Create(AText);
JSONValue2 := TJSONObject.ParseJSONValue(BText);
JSONObject2.AddPair(JSONString2, JSONValue2);
JSONArray.Add(JSONObject2);
JSONObject3.AddPair('People', JSONArray);
mStringStream := TStringStream.Create('', TEncoding.UTF8);
// m_StringStream.LoadFromStream(JSONObject3.ToString); <---ERROR
mStringStream.SaveToFile('people.json');
finally
JSONObject3.Free;
end;
end;
謝謝,我是json主題的初學者
uj5u.com熱心網友回復:
TJSONObject沒有任何流支持,但它有幾種To...()輸出方法(ToBytes(), ToJSON() ToString())。任何這些方法的輸出都可以寫入檔案,例如使用TFile.WriteAll...()方法 ( WriteAllBytes(), WriteAllText())。
試試這個:
uses
...,
Data.DBXJSON, // use System.JSON in XE6
System.IOUtils,
System.SysUtils;
procedure CreateJSON;
var
JSONObject, JSONObject2: TJSONObject;
JSONValue: TJSONValue;
JSONArray: TJSONArray;
AText, BText: string;
begin
JSONObject := TJSONObject.Create;
try
AText := 'Name';
BText := '"Charles"';
JSONValue := TJSONObject.ParseJSONValue(BText);
if JSONValue <> nil then
try
JSONObject.AddPair(AText, JSONValue);
except
JSONValue.Free;
raise;
end;
JSONArray := TJSONArray.Create;
try
JSONObject2 := TJSONObject.Create;
try
JSONArray.Add(JSONObject2);
except
JSONObject2.Free;
raise;
end;
JSONObject.AddPair('People', JSONArray);
except
JSONArray.Free;
raise;
end;
TFile.WriteAllText('people.json', JSONObject.ToJSON, TEncoding.UTF8);
finally
JSONObject.Free;
end;
end;
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/390509.html
標籤:json 德尔福 delphi-xe4
下一篇:Json模式陣列大小參考
