![]() ![]() JSONArray result = CDL.toJSONArray(ja, string) We have an alternative way of doing this where we can achieve the same functionality by supplying a JSONArray to get the headers and a comma delimited String working as the data.Īgain, we separate different lines using a carriage return (\r) or line feed (\n): JSONArray ja = new JSONArray() Notice that both data and header were supplied within the same String. The object JSONArray result now consists of the following (output formatted for the sake of clarity): [ JSONArray result = CDL.toJSONArray(string) The first line is interpreted as a list of headers, and all the subsequent lines are treated as data: String string = "name, city, age \n" + This method is overloaded and supports a wide range of data types.įor a complete list of methods supported by JSONArray, visit the official documentation. put(Object value) – append an object value to this JSONArray.If there's no value at that index, then a null is returned. opt(int index) – returns the value associated with an index (between 0 and total length – 1).get(int index) – returns the value at the specified index (between 0 and total length – 1), otherwise throws a JSONException.These are the primary methods of the JSONArray class: Like JSONObject, it has a constructor that accepts a source String and parses it to construct a JSONArray.It's represented by a String wrapped within square brackets and consists of a collection of values separated by commas.Values can be anything from a Number, String, Boolean, JSONArray, or JSONObject to even a JSONObject.NULL object.Since you seem to be flexible in choice of JSON parsing library I would suggest Jackson 2 from com.fasterxml instead of Jackson 1 from org.codehaus that you tried.A JSONArray is an ordered collection of values, resembling Java's native Vector implementation. Basically with findValue you can get another node deep into the tree, with get you can get array elements by index and with asText etc you parse a value in JSON into the appropriate type in Java. Assuming the order of ColumnNames is fixed then you can parse with Jackson like this: final ObjectMapper mapper = new ObjectMapper() įinal JsonNode tree = mapper.readTree(json) įinal JsonNode values = tree.findValue("Values") įinal ClientPOJO client = new ClientPOJO() ĬtDocumentId(node.get(1).asText()) ĬtScoredLabels(node.get(2).asBoolean()) ĬtScoredProbabilities(node.get(3).asDouble()) ĭocs for JsonNode. ![]() ![]() You can achieve this by getting the raw tree of data in this JSON and constructing objects by iterating over the Values array inside this tree. Neither Jackson nor any other library will parse the Values array into objects with client data like your POJO. ![]() HashMap map = omJson("your_json_string_here", type) I try it like this: public class ClientPOJO. The best case for me would be something like this, which I've done in another case: String uId = (String) attr.get("userId") String CN = (String) map.get("ColumnNames") īut then I get the following error: Exception in thread "main" : Unexpected character ('A' (code 65)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') I have tried it with something like this: Map map = mapper.readValue(filename, Map.class) "Values":, ]Īnd I want to have only the ColumnNames and the Values. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |