From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: =?UTF-8?Q?Bj=c3=b6rn_Lundin?= Newsgroups: comp.lang.ada Subject: Re: GNATCOLL JSON Parsing Date: Fri, 8 Jun 2018 11:35:19 +0200 Organization: A noiseless patient Spider Message-ID: References: <9524b3ee-476f-4af6-ab83-b15a6c2a417c@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Date: Fri, 8 Jun 2018 09:45:12 -0000 (UTC) Injection-Info: reader02.eternal-september.org; posting-host="c4c62f633dccda13764ab13818452bdf"; logging-data="7001"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/xY1ZNZV0oBsktsDJ6/ZOZ" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 Cancel-Lock: sha1:QLTUBP4SRsVnJgZDxV4CQp8FwdY= In-Reply-To: <9524b3ee-476f-4af6-ab83-b15a6c2a417c@googlegroups.com> Xref: reader02.eternal-september.org comp.lang.ada:52985 Date: 2018-06-08T11:35:19+02:00 List-Id: On 2018-06-08 07:52, eduardsapotski@gmail.com wrote: > I try understand parsing JSON in Ada. > > For example: > Have web-api that gives simple JSON: http://api.exmo.com/v1/trades/?pair=BTC_USD&limit=10 > > I need to save this data to database. > Created type: > > type Money is delta 0.00000001 range 0.0 .. 9_999_999_999.9; > type UTC_Date is range 1_500_000_000 .. 3_000_000_000; > > type Trade is record > > Trade_Id : Integer; > Pair : Unbounded_String; > Trade_Type : Unbounded_String; > Price : Money; > Quantity : Money; > Amount : Money; > Date : UTC_Date; > Saved : Boolean; > > end record; > > Created collection: > > package Vector_Trades is new Ada.Containers.Vectors(Natural, Trade); > > Trades : Vector_Trades.Vector; > > Receive data: > > JSON : Unbounded_String; > > JSON := To_Unbounded_String(AWS.Response.Message_Body (AWS.Client.Get (URL => "http://api.exmo.com/v1/trades/?pair=BTC_USD&limit=10"))); > > What to do next? How to get list of objects from the JSON-text? something like (not tested): declare use gnatcoll.Json; current_item, Reply : JSON_Value := Create; BTC_Array : json_array := empty_array; begin Reply := Read(Strm => Aws.Response.Message_Body(AWS_Reply),Filename => ""); if reply.has_Field("BTC_USD") then btc_array := reply.Get("BTC_USD"); if length(btc_array) > 0 then for i in 1 .. length(btc_array) loop Current_Item := Get(btc_array, i); declare tradeid : integer := 0; begin if Current_item.Has_Field("tradeid") then tradeid := current_item.Get(""tradeid); end if; ... end; end loop; end if; end if; end; > > How to save data to database already understood. > > Thanks. > > > > > > -- -- Björn