How do I save a physical .json file to my C: \ drive starting from my JSONObject3 object?
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;
Thank you, I am a beginner with the json topic
TJSONObjectdoes not have any streaming support, but it does have severalTo...()output methods (ToBytes(),ToJSON()ToString()). The output of any of those methods can be written to a file, such as withTFile.WriteAll...()methods (WriteAllBytes(),WriteAllText()).Try this instead: