Jump to content

wright

Members
  • Content Count

    40
  • Joined

  • Last visited

Everything posted by wright

  1. Hi there, i tried to parse the selected elements (JSON items), and then display the results using the [TListView](http://docwiki.embarcadero.com/Libraries/en/Vcl.ComCtrls.TListView) component and using using the [TJSONIterator.Next](http://docwiki.embarcadero.com/Libraries/en/System.JSON.Builders.TJSONIterator.Next), [TJSONIterator.Recurse](http://docwiki.embarcadero.com/Libraries/en/System.JSON.Builders.TJSONIterator.Recurse), and [TJSONIterator.Return](http://docwiki.embarcadero.com/Libraries/en/System.JSON.Builders.TJSONIterator.Return) methods. Code data: {"address": {"building": "1007", "coord": [-73.856077, 40.848447], "street": "Morris Park Ave", "zipcode": "10462"}, "borough": "Bronx", "cuisine": "Bakery", "grades": [{"date": {"$date": 1393804800000}, "grade": "A", "score": 2}, {"date": {"$date": 1378857600000}, "grade": "A", "score": 6}, {"date": {"$date": 1358985600000}, "grade": "A", "score": 10}, {"date": {"$date": 1322006400000}, "grade": "A", "score": 9}, {"date": {"$date": 1299715200000}, "grade": "B", "score": 14}], "name": "Morris Park Bake Shop", "restaurant_id": "30075445"} {"address": {"building": "469", "coord": [-73.961704, 40.662942], "street": "Flatbush Avenue", "zipcode": "11225"}, "borough": "Brooklyn", "cuisine": "Hamburgers", "grades": [{"date": {"$date": 1419897600000}, "grade": "A", "score": 8}, {"date": {"$date": 1404172800000}, "grade": "B", "score": 23}, {"date": {"$date": 1367280000000}, "grade": "A", "score": 12}, {"date": {"$date": 1336435200000}, "grade": "A", "score": 12}], "name": "Wendy'S", "restaurant_id": "30112340"} {"address": {"building": "351", "coord": [-73.98513559999999, 40.7676919], "street": "West 57 Street", "zipcode": "10019"}, "borough": "Manhattan", "cuisine": "Irish", "grades": [{"date": {"$date": 1409961600000}, "grade": "A", "score": 2}, {"date": {"$date": 1374451200000}, "grade": "A", "score": 11}, {"date": {"$date": 1343692800000}, "grade": "A", "score": 12}, {"date": {"$date": 1325116800000}, "grade": "A", "score": 12}], "name": "Dj Reynolds Pub And Restaurant", "restaurant_id": "30191841"} {"address": {"building": "2780", "coord": [-73.98241999999999, 40.579505], "street": "Stillwell Avenue", "zipcode": "11224"}, "borough": "Brooklyn", "cuisine": "American ", "grades": [{"date": {"$date": 1402358400000}, "grade": "A", "score": 5}, {"date": {"$date": 1370390400000}, "grade": "A", "score": 7}, {"date": {"$date": 1334275200000}, "grade": "A", "score": 12}, {"date": {"$date": 1318377600000}, "grade": "A", "score": 12}], "name": "Riviera Caterer", "restaurant_id": "40356018"} {"address": {"building": "97-22", "coord": [-73.8601152, 40.7311739], "street": "63 Road", "zipcode": "11374"}, "borough": "Queens", "cuisine": "Jewish/Kosher", "grades": [{"date": {"$date": 1416787200000}, "grade": "Z", "score": 20}, {"date": {"$date": 1358380800000}, "grade": "A", "score": 13}, {"date": {"$date": 1343865600000}, "grade": "A", "score": 13}, {"date": {"$date": 1323907200000}, "grade": "B", "score": 25}], "name": "Tov Kosher Kitchen", "restaurant_id": "40356068"} procedure TForm2.AddColumn(const AName: String); var oCol: TListColumn; begin oCol := ListView1.Columns.Add; oCol.Width := -1; oCol.Caption := AName; end; procedure TForm2.Button3Click(Sender: TObject); begin ParseObject; end; procedure TForm2.ParseObject; var oIter: TJSONIterator; LJsonTextReader: TJsonTextReader; LStringReader: TStreamReader; oItem: TListItem; I: Integer; begin // oIter := TJSONIterator.Create(LJsonTextReader); // NObjJSON := oIter.AsInteger; ListView1.Items.Clear; ListView1.Columns.Clear; ListView1.ViewStyle := vsReport; AddColumn('Name'); AddColumn('Cuisine'); AddColumn('Street'); AddColumn('Building'); AddColumn('Borough'); ListView1.Items.BeginUpdate; try oItem := ListView1.Items.Add; for i := 1 to ListView1.Columns.Count - 1 do oItem.SubItems.Add(''); LStringReader := TStreamReader.Create('../../resto.json', TEncoding.UTF8, True); LJsonTextReader := TJsonTextReader.Create(LStringReader); oIter := TJSONIterator.Create(LJsonTextReader); try while True do begin while oIter.Next do if oIter.&Type in [TJsonToken.StartObject, TJsonToken.StartArray] then oIter.Recurse else if oIter.Path = 'name' then oItem.Caption := oIter.AsString else if oIter.Path = 'cuisine' then oItem.SubItems[0] := oIter.AsString else if oIter.Path = 'address.street' then oItem.SubItems[1] := oIter.AsString else if oIter.Path = 'address.building' then oItem.SubItems[2] := oIter.AsString else if oIter.Path = 'borough' then oItem.SubItems[3] := oIter.AsString; if oIter.InRecurse then oIter.Return else Break; end; finally oIter.Free; LJsonTextReader.Free; LStringReader.Free; end; finally ListView1.Items.EndUpdate; end; end; As Result: i only got one line filled. I would like to get the result like that: Expected behavior
  2. Got it Works but in a different way than the official sample. I'm not satisfied with my procedure, i think there is a better way to do iteration without MongoDB. what i changed: ... ListView1.Items.BeginUpdate; try LStreamReader := TStreamReader.Create('../../resto.json', TEncoding.UTF8, True); LJsonTextReader := TJsonTextReader.Create(LStreamReader); oIter := TJSONIterator.Create(LJsonTextReader); while oIter.Next() do begin oItem := ListView1.Items.Add; for i := 1 to ListView1.Columns.Count - 1 do oItem.SubItems.Add(''); if oIter.&Type in [TJsonToken.StartObject, TJsonToken.StartArray] then begin oIter.Recurse; if oIter.Next('address') then begin oIter.Recurse; oIter.Next('building'); oItem.SubItems[2] := oIter.AsString; oIter.Next('street'); oItem.SubItems[1] := oIter.AsString; oIter.Return end; oIter.Next('borough'); oItem.SubItems[3] := oIter.AsValue.ToString; oIter.Next('cuisine'); oItem.SubItems[0] := oIter.AsValue.ToString; oIter.Next('name'); oItem.Caption := oIter.AsValue.ToString; end; if oIter.InRecurse then oIter.Return else Break; end; finally ListView1.Items.EndUpdate; end; ... Result:
  3. Hello guys! @Angus Robertson, any progress?
  4. It seems that TJSONIterator.Path doesn't read the values at all! i tried this and i still figure issues: cuisine doens't load. ............ ListView1.Items.BeginUpdate; try LStreamReader := TStreamReader.Create('../../resto.json', TEncoding.UTF8, True); LJsonTextReader := TJsonTextReader.Create(LStreamReader); oIter := TJSONIterator.Create(LJsonTextReader); while oIter.Next() do begin oItem := ListView1.Items.Add; for i := 1 to ListView1.Columns.Count - 1 do oItem.SubItems.Add(''); if oIter.&Type in [TJsonToken.StartObject, TJsonToken.StartArray] then begin oIter.Recurse; oIter.Next('name'); oItem.Caption := oIter.AsValue.ToString; oIter.Next('cuisine'); oItem.SubItems[0] := oIter.AsValue.ToString; end ......... .........
  5. Yes! i did it before i posted but the result was ennoying and shown 0 lines. that's the reason i used the Json data as default. NB: i forgot to tell that the json data i used is a piece of code from the restaurant project inside Rad Studio samples which uses MongoDB as database: * navigate to: Object Pascal\Database\FireDAC\Samples\DBMS Specific\MongoDB\Restaurants * original Json: Object Pascal\Database\FireDAC\DB\Data\restaurants.json
  6. Good news, Thank you! i appreciate!
  7. really weird. In the meantime i tried with trial version of "IPWorks ssl" all is fine, works very well. I Think i have to check inside ICS and do a proper client as you said in the upper comments! That's the tricky part!
  8. exactly what i did. via 2 sources: 1. https://en.wikipedia.org/wiki/WebSocket#Protocol_handshake 2. attached png but i don't know why in my case TCP proto doesn't work. im still learning delphi prog and websocket, i'll check again for a better workarround.
  9. Seems to work! i change proto to UDP. but i think it's not right way to solve it. i dig.
  10. Of course. i enabled ssl, socket familly: sfAny,
  11. As @Angus Robertson reported; so i followed what you suggested. i got no error know but no connection or it seems attempting to connect. (Port 80 even with 443)
  12. Hi, i forgot to said that i had already tried with only the hostname as you mentionned, but the error the same "cannot convert host address"
  13. Hello,I am learning to use the TJSONIterator(Readers and Writers JSON Framework) under delphi 10.3.3 and I found a russian link putting this into practice through the creation of an application assessing the negative impact on the environment: https://webdelphi.ru/. Firstly, i have a custom class definition named "NVOS.JsonParser.pas" and the "open-data-1.json" file, i don't know how to proceed However, I can't figure out already knowing that the result should look like: "App.png". open-data-1.json
×