Jump to content
MirDurmaz

Json create Array

Recommended Posts

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

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 by Remy Lebeau
  • Like 2

Share this post


Link to post

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×