Closed Jitterer closed 5 years ago
Hi,
1) could you show an example? You want to have java object with ArrayList from json array "cars":[ "Ford", "BMW", "Fiat" ]
?
2) Is it really needed when using this library? Everything should be already deserialized.
Hi Pengrad,
thx for your very quick response.
GetChatResponse{result=Chat{id=-xxx, type=group, first_name='null', last_name='null', username='null', title='xxx', all_members_are_administrators=true, photo=null, description='null', invite_link='null', pinned_message=null, permissions=ChatPermissions{can_send_messages=true, can_send_media_messages=true, can_send_polls=true, can_send_other_messages=true, can_add_web_page_previews=true, can_change_info=true, can_invite_users=true, can_pin_messages=true}, sticker_set_name='null', can_set_sticker_set=null}}
this seems (for my understanding) to be serialized. something with I cant work with. My current workaround is like creating a map and inserting with the available methods like m.put("rc", updates.errorCode()) etc. ne key=value pairs. But of course I just dont want to do it with every key value pair. So I'm searching to convert the response to an arraylist
Well, it's kind of serialized, but actually not :)
It's just a string version GetChatResponse
object which you get via printing or calling toString()
method
GetChatResponse getChatResponse = bot.execute(new GetChat(chatId));
System.out.println(getChatResponse);
And you can access any field of this object:
Chat chat = getChatResponse.chat();
Long id = chat.id();
String firstName = chat.firstName();
ChatPermissions permissions = chat.permissions();
And if some field represents json's array - it will be java array.
You can check more examples in Test class https://github.com/pengrad/java-telegram-bot-api/blob/master/library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java#L366
yes, I already found out that I can access the fields. But what i do NOT want to do is (what I currently doing), to create a new arraylist/map structure where I need to write every available method into a new key value map (like in my post before with map.put("rc", updates.errorCode()).
So I'm searching a comfortable way to directly convert the response as an arraylist (or even json or any way to work with but not as a string which I cant parse).
I'm very thankful for your help!
You can convert Java object back to json with Gson:
BaseResponse response = bot.execute(new GetChat(groupId));
String json = new Gson().toJson(response);
System.out.println(json);
Thx a lot! I never thought this way. Does work like a charm! I appreciate it!
Hi,
first thx for this project and your library! For information: I'm completely java beginner what is the reason for my question I think ;) Hopefully you will answer and help me out.
I have trouble to get the serialized list deserialized as an ArrayList. I'm trying everything for hours now. Could you help me out to get the serialized list deserialized with an completely accessible ArrayList? Thx in advance!
BR