Forum > Portuguese
Pegar itens de objeto dentro do json
TRon:
--- Quote from: Johnny Walker Junior on April 27, 2024, 08:36:29 pm ---I use Horse framework to do the API.
--- End quote ---
Thank you for the link.
As far as I am able to tell Horse uses the standard (free Pascal) fpjson units. I am unable to detect where this might have gone wrong.
Are you perhaps able to share an example that produces that resulting JSON string for you ?
At first glance (of the original issue) it seems that someone is trying to merge two different JSON strings (or objects) into one and which went wrong somehow. Do note that I am aware that some servers are able to provide a similar JSON response string but in such case they are also using JSON in a manner that isn't suppose to be used that way :)
edit:
A small example that is hopefully be able to show how things can go wrong (note that it is a console program but it can easily be adapted to work with a GUI application with Lazarus just remove/replace the writeln's or start Lazarus from a terminal window):
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program produce; {$mode objfpc}{$h+} uses classes, fpjson; procedure dodo;var Root : TJSONObject; Data : TJSOnArray; Item : TJSONObject; done_wrong: boolean = true; item_as_string : string; data_as_string : string; root_as_string : string;begin // create root node Root := TJSONObject.Create; // add some items to root node Root.Add('success', true); Root.Add('message', 'returned success'); // create data array Data := TJSONArray.Create; // create array item Item := TJSONObject.Create; // add items to object item Item.Add('id', 1); Item.Add('cpf', '99999890667'); Item.Add('name', 'Johnny'); writeStr(item_as_string, 'item = ', Item.AsJSON); // add array item to array if done_wrong then Data.Add(Item.AsJSON) // <--- this is wrong (add Item as JSON string) else Data.Add(Item); // <--- this is good (add item as JSON array) writeStr(data_as_string, 'data = ', Data.AsJSON); // add data array to root node Root.Add('data', Data); writeStr(root_as_string, 'root = ', Root.AsJSON); if done_wrong then Item.Free; Root.Free; // if on console: (else replace with writing to a memo or set caption of labels) writeln(item_as_string); writeln(data_as_string); writeln(root_as_string);end; begin dodo;end.
When gone_wrong boolean is set to true:
--- Code: ---item = { "id" : 1, "cpf" : "99999890667", "name" : "Johnny" }
data = ["{ \"id\" : 1, \"cpf\" : \"99999890667\", \"name\" : \"Johnny\" }"]
root = { "success" : true, "message" : "returned success", "data" : ["{ \"id\" : 1, \"cpf\" : \"99999890667\", \"name\" : \"Johnny\" }"] }
--- End code ---
Also note that even if this comes close to your original JSON string this examples does not (even) encapsulate the array itself between quotes.
When gone_wrong boolean is set to false:
--- Code: ---item = { "id" : 1, "cpf" : "99999890667", "name" : "Johnny" }
data = [{ "id" : 1, "cpf" : "99999890667", "name" : "Johnny" }]
root = { "success" : true, "message" : "returned success", "data" : [{ "id" : 1, "cpf" : "99999890667", "name" : "Johnny" }] }
--- End code ---
Which is how the JSON string should look like.
Johnny Walker Junior:
I was using this YouTube channel below to learn about this framework, it has English subtitles, so there is no problem understanding what the person is saying.
In the case of the server in console mode, there is no problem, I also run it in console.
https://www.youtube.com/@Alexandre_amds/videos
Thank you very much my friend
bye
TRon:
--- Quote from: Johnny Walker Junior on April 28, 2024, 11:08:23 pm ---I was using this YouTube channel below to learn about this framework, it has English subtitles, so there is no problem understanding what the person is saying.
--- End quote ---
Unfortunately the only subtitle language I am able to see are Portuguese :'(
But... it is currently unclear to me if you still have this problem or that you were able to get it solved.
Johnny Walker Junior:
My friend it's possible to translate to English, just turn on "subtitles/cc" and "automatically translate" to the specified language.
bye
TRon:
--- Quote from: Johnny Walker Junior on April 29, 2024, 09:53:20 pm ---My friend it's possible to translate to English, just turn on "subtitles/cc" and "automatically translate" to the specified language.
--- End quote ---
Yes, I am aware of that but are you aware how terrible auto-translate works (or actually not works :) ) ?
But it is also a bit besides the point.
I had a look at the framework and I understand the concepts that are used as well as the implementation.
The part were things seem to go wrong is when the framework is applied in practice. Ergo, the end-user who made use of the framework made a mistake somewhere along the line which resulted in that JSON string.
I am unable to replicate that because I am not the person who created the code and I will not be able to make that exact same mistake because I understand how JSON works (famous last words :) ).
That is why I posted an example using fpjson (which the Horse Framework can use as well) that is able to produce such 'faulty' JSON strings. I did that in the hopes that you perhaps are able to recognize the (faulty) pattern so that you are able to figure out where things go wrong for you.
It is also (still) unclear for me if you are creating the 'faulty' JSON string yourself and if so whether or not that was done locally or if the faulty JSON string is produced by a server (and in case the latter if you coded the server yourself or if you retrieved that faulty JSON string from an external server that is written by someone else)
So, I actually have more questions than I have answers, I am afraid... :-\
Navigation
[0] Message Index
[#] Next page
[*] Previous page