简体   繁体   中英

String comma delimited to JsonArray

I am using delphi xe7, win8.1

My question is simple, but I am a newbie in Json.

I have a comma delimited string to convert to JSONArray.

var s:string;
    ja:JSONArray;
begin
s:='a,b,c,d,e,f';//--> can have many items comma delimited.
ja:=JSONArray.Create;
//???Convert string comma delimited to JSONArray
ja:=ConvertStringDelimitedToJsonArray(s);
end;

I´d like to know how to code the function ConvertStringDelimitedToJsonArray with a string as input parameter and output a JSONArray.

Is there a delphi function to do it directly?

A JSON array is just a list of element divided by comma and enclosed by square brackets, so I think that given your input data, you could simply do something like this:

s := 'a,b,c,d,e,f';
s := Format("[%s]", [s]);
ja := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(s),0) as TJSONArray;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM