MirDurmaz 0 Posted September 19, 2022 Hi EverBody, I have a problem a create json.I don't create a json array without name property. I could create below json: { "registration_ids" : [ "crX7c-FfQXunMB5dqoaaUM:APA91brRYBuOESgGzWKmLg0M" ], "notification" : { "title": "Hi", "body" : "Notification test" } } My delphi code: wrtString := TStringWriter.Create(); wrtJSON := TJsonTextWriter.Create(wrtString); try wrtJSON.Formatting := TJsonFormatting.Indented; wrtJson.WriteStartObject; wrtJson.WritePropertyName('registration_ids'); wrtJson.WriteStartArray; wrtJson.WriteStartObject; wrtJson.WritePropertyName(''); wrtJson.WriteValue(strToken); wrtJson.WriteEndObject; wrtJson.WriteEndArray; wrtJson.WritePropertyName('notification'); wrtJSON.WriteStartObject; wrtJSon.WritePropertyName('title'); wrtJson.WriteValue(edtBaslik.Text); wrtJson.WritePropertyName('body'); wrtJson.WriteValue(edtMesaj.Text); wrtJSON.WriteEndObject; wrtJSON.WriteEndObject; and my json: { "registration_ids": [ { "": "crX7c-FfQXunMB5dqoaaUM:APA91bHOuvOioxEjciJS9V04g9" ], "notification": { "title": "Hello", "body": "Delphi" } } Share this post Link to post
Remy Lebeau 1394 Posted September 19, 2022 (edited) In the JSON you want to create, "registration_ids" is an array of strings. But in your code, you are creating "registration_ids" as an array of objects instead. To get the result you want, between WriteStartArray() and WriteEndOfArray(), you need to get rid of WriteStartObject(), WritePropertyName(''), and WriteEndObject(): wrtString := TStringWriter.Create(); wrtJSON := TJsonTextWriter.Create(wrtString); try wrtJSON.Formatting := TJsonFormatting.Indented; wrtJson.WriteStartObject; wrtJson.WritePropertyName('registration_ids'); wrtJson.WriteStartArray; //wrtJson.WriteStartObject; // <-- //wrtJson.WritePropertyName(''); // <-- wrtJson.WriteValue(strToken); //wrtJson.WriteEndObject; // <-- wrtJson.WriteEndArray; wrtJson.WritePropertyName('notification'); wrtJSON.WriteStartObject; wrtJSon.WritePropertyName('title'); wrtJson.WriteValue(edtBaslik.Text); wrtJson.WritePropertyName('body'); wrtJson.WriteValue(edtMesaj.Text); wrtJSON.WriteEndObject; wrtJSON.WriteEndObject; That said, why are you using TJsonTextWriter at all? This would be much more straight-forward if you used TJSONObject and TJSONArray instead, eg: uses ..., System.JSON; var arr: TJSONArray; notif, obj: TJSONObject; strJSON: string; begin obj := TJSONObject.Create; try arr := TJSONArray.Create; try arr.Add(strToken); obj.AddPair('registration_ids', arr); except arr.Free; raise; end; notif := TJSONObject.Create; try notif.AddPair('title', 'Hi'); notif.AddPair('body', 'Notification test'); obj.AddPair('notification', notif); except notif.Free; raise; end; strJSON := obj.ToJSON; // or, obj.Format finally obj.Free; end; // use strJSON as needed... end; Edited September 20, 2022 by Remy Lebeau 2 Share this post Link to post
Fesih ARSLAN 1 Posted September 19, 2022 A different solution: https://www.delphican.com/showthread.php?tid=6936&pid=50276#pid50276 Share this post Link to post
stijnsanders 35 Posted September 24, 2022 So much code! Are you willing to try out a different JSON solution? When I was writing my very bare-bones connector to MongoDB, I was really into the Variant type, and really disliked long lists of overloads for all of the types. So I created my own JSON solution, that heavily uses the Variant type, and offers a really concise syntax. The above would look like this: JSON(['registration_ids',VarArrayOf(['crX...']),'notification',JSON(['title','Hi','body','Notification test'])]) Share this post Link to post
mytbo 5 Posted September 24, 2022 With mORMot you can write it as follows: ShowMessage(Utf8ToString(VariantSaveJson( _Obj(['registration_ids', _Arr(['crX7c-...']), 'notification', _Obj(['title', 'Hi', 'body', 'Notification test'])])))); The function _Obj() uses name-value pairs. The description of _Obj() can be found in the help here and for _Arr() here. Information about TDocVariant documents can be found here. With best regards Thomas Share this post Link to post