parsing a json file without knowing it structure using only java [duplicate] - java

This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 9 months ago.
so I have, I doing a program that receives a string json with an unknown structure (since it's a variable each time I receive a new one) and it needs to parse the string to extract each key and its type (from its value).
I need to do this using only java (no spring) because I've seen solution using Jackson tree, but with the software I'm using it impossible
If someone could help I would appreciate it.

You can use JSONObject from org.json library .

Related

How would i parse this with Gson? [duplicate]

This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 2 years ago.
I am trying to make a discord bot that parses a json response, but I'm not entirely sure on how to do so.
This is an example response:
[{"beatmap_id":"146929","score":"1467874","maxcombo":"182","count50":"1","count100":"41","count300":"475","countmiss":"10","countkatu":"20","countgeki":"72","perfect":"0","enabled_mods":"0","user_id":"1009","date":"2020-02-29 11:15:49","rank":"A","pp":"38.767826"},{"beatmap_id":"245658","score":"1013358","maxcombo":"244","count50":"0","count100":"6","count300":"158","countmiss":"1","countkatu":"5","countgeki":"24","perfect":"0","enabled_mods":"0","user_id":"1009","date":"2020-02-29 11:13:05","rank":"A","pp":"49.74563"},{"beatmap_id":"245658","score":"34162","maxcombo":"42","count50":"0","count100":"1","count300":"27","countmiss":"1","countkatu":"0","countgeki":"4","perfect":"0","enabled_mods":"0","user_id":"1009","date":"2020-02-29 11:12:19","rank":"A","pp":"12.399677"},{"beatmap_id":"1128411","score":"4785282","maxcombo":"350","count50":"23","count100":"292","count300":"1117","countmiss":"51","countkatu":"93","countgeki":"132","perfect":"0","enabled_mods":"0","user_id":"1009","date":"2020-02-29 11:10:52","rank":"C","pp":"12.770388"},{"beatmap_id":"431147","score":"209504","maxcombo":"105","count50":"0","count100":"4","count300":"73","countmiss":"1","countkatu":"3","countgeki":"9","perfect":"0","enabled_mods":"0","user_id":"1009","date":"2020-02-29 11:04:05","rank":"A","pp":"22.695227"}]
How would I just get the first "beatmap_id" key and return the value of it with Gson?
I'm fairly new to coding and I need a push in the right direction, so could someone give me an example, or link me to the part of the documentation i need to learn this?
Any help is appreciated, thanks!
You should create DTO class for same followed by keys which are in the response.
You can view this blog for understanding gson.

Convert XML Node (w3c) to String XML representation [duplicate]

This question already has answers here:
XML Node to String in Java
(2 answers)
Convert an org.w3c.dom.Node into a String
(3 answers)
How to read and write XML files and treat the comment nodes as text nodes in Java when saving
(1 answer)
Closed 3 years ago.
I am desperately looking for a library or snippet to convert a XML Node into a string representing the Node element.
I only find String > XML guides. I found the Spring Shell library XmlUtils which had that functionality (in version v1.0.0.M1) but was broken due transient dependencies and therefore not possible to be used.
I also found Apache XMLUtils which have a similar functionality. Sadly the lib seems to be not maintained for more than 8 years or such. I am unsure to use it because of that.
EDIT: I found this one as the actual XMLUtils from Apache Axis2: https://mvnrepository.com/artifact/org.apache.axis2/axis2-kernel/1.7.9
But as I can see in the doc the wanted method has been removed (https://axis.apache.org/axis2/java/core/apidocs/org/apache/axis2/util/XMLUtils.html)
Did I missed something? Every Utils library I found has removed the functionality of converting an Element/Node into a String XML.
Am really in need of a good advice for the mentioned problem.

convert CSV file to JSON in java [duplicate]

This question already has answers here:
Converting an CSV file to a JSON object in Java
(9 answers)
Closed 6 years ago.
This is my CSV file:
no name id
1 yog a122
2 nik b122
I want convert this file in json using java. I tried javascript, jsp,
servlet but didn't get output.
I think you need to bit more googling on this, that is pretty much famous question.
Please check following link
https://github.com/FasterXML/jackson-dataformat-csv
And also following stack overflow post
directly convert CSV file to JSON file using the Jackson library

how to parse unusual json [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Parse JSON object with string and value only
How to parse JSON in JavaScript
How can I parse such json?
"prices":{"2":"800.0", "8":"580.0", "5":"657.0"}
Usually I do it in this way:
object.getInt("id")
But now I don't know previously what int I get. In example there are 2,8,5, but they can be another..
Thanks in advance.
Here is answer
You need a JSON library for Java.

json to java object convertion [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert a JSON string to object in Java?
Below is my json structure .I want to convert it to java object .Can any help me to parse this jason object
{"response":{"#id":"","":{"":[{"":"","":"","":"","":"","":"","":"","":"","":"","":"","":""},{},{},{},.....{}]}}}
Thanks
That is not well-formed JSON. For a start, you have multiple attributes with the same name. There's no Java JSON library that could represent that.

Categories

Resources