Skrim 11 Posted November 13, 2021 (edited) Hi I am struggling parsing this Jsonfile. I can parse all of it's values, except from the array. This is where I fail: "adresse": ["Some text"], As I understand it this is an array. Code for parsing text elements: companyinfo:=tjsonobject.parsejsonvalue(restresponse1.Content) as tjsonobject; s:=companyinfo.GetValue('navn').Value; ss:=companyinfo.GetValue<string>('forretningsadresse'+ '.' + 'postnummer'); s now has the value of the string, aCompanyname LTD ss now has the value of the string, 3616 Help with parsing the array would be greatly appreciated. Here is the Json: { "organisasjonsnummer": "123456789", "navn": "aCompanyname LTD", "organisasjonsform": { "kode": "LTD", "beskrivelse": "Aksjeselskap", "_links": { "self": { "href": "https://..alink..." } } }, "registreringsdatoEnhetsregisteret": "1995-02-20", "registrertIMvaregisteret": true, "naeringskode1": { "beskrivelse": "Some text", "kode": "69.201" }, "antallAnsatte": 2, "forretningsadresse": { "land": "Norge", "landkode": "NO", "postnummer": "3616", "poststed": "KONGSBERG", "adresse": [ "Some text" ], "kommune": "KONGSBERG", "kommunenummer": "3006" }, "stiftelsesdato": "1994-06-06", "institusjonellSektorkode": { "kode": "2100", "beskrivelse": "Private aksjeselskaper mv." }, "registrertIForetaksregisteret": true, "registrertIStiftelsesregisteret": false, "registrertIFrivillighetsregisteret": false, "sisteInnsendteAarsregnskap": "2020", "konkurs": false, "underAvvikling": false, "underTvangsavviklingEllerTvangsopplosning": false, "maalform": "Bokmål", "_links": { "self": { "href": "https://...a link..." } } } Edited November 14, 2021 by Skrim Share this post Link to post
Skrim 11 Posted November 14, 2021 Modified Json { "forretningsadresse":{ "land":"Norge", "landkode":"NO", "postnummer":"3616", "poststed":"KONGSBERG", "adresse":[ "Storgata 8" ] } } I have tried this code, but jsonArray is always nil. I quess I'm not refering correctly to the Json? var jsonObj: TJsonObject; jsonArray: TJsonArray; begin jsonObj:=TJsonObject.ParseJSONValue(memo1.Lines.Text) as TJsonObject; // Json in memo1 jsonArray:=jsonObj.GetValue('forretningsadresse'+ '.' + 'adresse') as TJsonArray; if jsonArray<>nil then begin ... end; ... end; Share this post Link to post
Attila Kovacs 629 Posted November 14, 2021 for example: (jsonObj.Values['forretningsadresse'] as TJSONObject).Values['adresse'] Share this post Link to post
Skrim 11 Posted November 14, 2021 1 hour ago, Attila Kovacs said: for example: (jsonObj.Values['forretningsadresse'] as TJSONObject).Values['adresse'] Thanks for your answer. You mean, jsonarray:=(jsonObj.Values['forretningsadresse'] as TJSONObject).Values['adresse']; It will not compile. I'm sorry, my knowledge/understanding of this topic is very poor. Share this post Link to post
Attila Kovacs 629 Posted November 14, 2021 maybe "adresse'] as TJSONArray);" the return type of Values[] is very general, you have to process it with checking against the type and cast them if apply. Share this post Link to post
boris.nihil 0 Posted November 15, 2021 var JsonValueArr,JsonValue: TJSONValue; JsonObject: TJSONObject; jasonArr: TJSONArray; begin JsonValue := TJSONObject.ParseJSONValue(memoRequest.Lines.Text); if JsonValue = nil then begin ShowMessage('Error'); end else begin JsonObject := JsonValue as TJSONObject; JsonObject := GetObject(JsonObject, 'forretningsadresse'); jasonArr := GetValue(JsonObject, 'adresse') as TJSONArray; for JsonValueArr in jasonArr do begin showmessage(JsonValueArr.Value); end; end; end; Share this post Link to post
boris.nihil 0 Posted November 15, 2021 function GetValue(JsonObject: TJSONObject; const PairName: string): TJSONValue; var JsonPair: TJSONPair; begin JsonPair := JsonObject.Get(PairName); if JsonPair = nil then raise Exception.Create('Pair "' + PairName + '" not found'); Result := JsonPair.JsonValue; end; function GetObject(JsonObject: TJSONObject; const PairName: string): TJSONObject; begin Result := GetValue(JsonObject, PairName) as TJSONObject; end; Share this post Link to post
Remy Lebeau 1394 Posted November 15, 2021 (edited) 1 hour ago, boris.nihil said: JsonValue := TJSONObject.ParseJSONValue(memoRequest.Lines.Text); Don't forget to call JsonValue.Free() when you are done using JsonValue, or else it will be leaked. This is a common use-case for a try..finally block. Edited November 15, 2021 by Remy Lebeau Share this post Link to post
Skrim 11 Posted November 15, 2021 2 hours ago, boris.nihil said: var JsonValueArr,JsonValue: TJSONValue; JsonObject: TJSONObject; jasonArr: TJSONArray; begin JsonValue := TJSONObject.ParseJSONValue(memoRequest.Lines.Text); if JsonValue = nil then begin ShowMessage('Error'); end else begin JsonObject := JsonValue as TJSONObject; JsonObject := GetObject(JsonObject, 'forretningsadresse'); jasonArr := GetValue(JsonObject, 'adresse') as TJSONArray; for JsonValueArr in jasonArr do begin showmessage(JsonValueArr.Value); end; end; end; A big thank you to you Boris, also to Remy. I also read this thread with great interest, New to JSON. In addition to the code above I had to paste GetObject and GetValue from New to Json. I could not have done this on my own. Share this post Link to post
boris.nihil 0 Posted November 16, 2021 16 hours ago, Skrim said: A big thank you to you Boris, also to Remy. I also read this thread with great interest, New to JSON. In addition to the code above I had to paste GetObject and GetValue from New to Json. I could not have done this on my own. all thanks goes to Remy Share this post Link to post