1

How can I insert or add a new Field to JSON file ? I'm using lkjson btw.

var
  js: TlkJSONobject;
  val : String;
begin
  js := TlkJSONstreamed.loadfromfile(jsonFile) as TlkJsonObject;
  try
    try
      val := js.Field['users'].Field[username].Field[value5].Value; //field value5 doesnt exist
    except
      //add field

    end;
    TlkJSONstreamed.SaveToFile(js, jsonFile);
  finally
    js.Free;
  end;
end;
Area50plus1
  • 111
  • 2
  • 7
  • 1
    if you want this lazy approach with implicit creation I think you should use SuperObject library instead. While it might be somewhat slower. / http://stackoverflow.com/questions/16790006 / http://stackoverflow.com/questions/7731833 – Arioch 'The Nov 27 '15 at 08:02

1 Answers1

1

Like this:

(js.Field['users'].Field[username] as TlkJSONobject).Add(value5, '1');

Where users and username do exist.

fantaghirocco
  • 4,761
  • 6
  • 38
  • 48