I am trying to parse this JSONObject
{
"query": {
"yahoo:count": 1,
"results": {
"rate": {
"Name": "USD/INR",
"id": "USDINR",
"Time": "12:19pm",
"Date": "10/31/2015",
"Bid": 65.405,
"Ask": 65.43,
"Rate": 65.405
}
},
"yahoo:created": "2015-11-01T02:16:56Z",
"yahoo:lang": "en-US",
"xmlns:yahoo": "http://www.yahooapis.com/v1/base.rng"
}
}
This is my program
import java.text.ParseException;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Test {
public static void main(String[] args) throws ParseException, JSONException {
String json = "{\"query\":{\"yahoo:count\":1,\"results\":{\"rate\":{\"Name\":\"USD/INR\",\"id\":\"USDINR\",\"Time\":\"12:19pm\",\"Date\":\"10/31/2015\",\"Bid\":65.405,\"Ask\":65.43,\"Rate\":65.405}},\"yahoo:created\":\"2015-11-01T02:16:56Z\",\"yahoo:lang\":\"en-US\",\"xmlns:yahoo\":\"http://www.yahooapis.com/v1/base.rng\"}}";
JSONObject json_obj = new JSONObject(json);
String Rate = json_obj.getJSONObject("query").getJSONObject("results")
.getJSONObject("rate").getString("Rate");
System.out.println(Rate);
}
}
Exception in thread "main" org.json.JSONException: JSONObject["Rate"] not a string.
at org.json.JSONObject.getString(JSONObject.java:644)
at Test.main(Test.java:16)
Could you please let me know how to resolve this ??
Its strict about its types, getString("Rate") is not a string its a number. To get the number value use getDouble("rate"). You can also use the type safe get of get("rate") which should return a Double then call toString() on it.
Isn't the stack trace obvious? Rate is not a string, but an integer. You should use getInt or getNumber or whatever is called in Java, instead of getString.
Try like this
.getDouble("Rate")
instead of
.getString("Rate")
rate is double not string
Related
I use an API from api.nasa.gov that I query using the following Java class to read magnitude information on the latest solar flare of the day.
The response is used to fill in a field on a weather station that informs about current solar hazards.
The problem is that this code only returns the "classType" (magnitude) of first "flrID" (solar flare) event of the day. On July 3, for example, there was more than one event, and the most relevant event is the latest.
I am trying to find out how to get the "classType" of the last "flrID" in the JSON string, given that the flrID text contains an unknown arbitrary time of day.
Secondly, I am not sure how to get event driven updates from NASA, other than from pushed email messages. I am allowed an API request every 3.6 seconds, so that will work, but I am inquiring if there is a less expensive method to get near real time updates from NASA.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import org.json.JSONException;
import org.json.JSONObject;
public class NasaFlrApiReader {
public static final String API_KEY = "DEMO_KEY";
private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
try (InputStream is = new URL(url).openStream()) {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
String jsonText = readAll(rd);
int i = jsonText.indexOf("{");
jsonText = jsonText.substring(i);
JSONObject json = new JSONObject(jsonText);
return json;
}
}
public static void main(String[] args) throws IOException, JSONException {
JSONObject json = readJsonFromUrl("https://api.nasa.gov/DONKI/FLR?startDate=2021-07-27-03&endDate=2021-07-03&api_key=" + API_KEY);
System.out.println("Class Type: " + json.get("classType"));
}
}
This is the JSON text that is returned from the above code:
[
{
"flrID": "2021-07-03T02:14:00-FLR-001",
"instruments": [
{
"displayName": "GOES-P: EXIS 1.0-8.0"
}
],
"beginTime": "2021-07-03T02:14Z",
"peakTime": "2021-07-03T02:31Z",
"endTime": "2021-07-03T02:39Z",
"classType": "C5.7",
"sourceLocation": "N23W75",
"activeRegionNum": 12838,
"linkedEvents": [
{
"activityID": "2021-07-03T03:48:00-CME-001"
}
],
"link": "https://kauai.ccmc.gsfc.nasa.gov/DONKI/view/FLR/17197/-1"
},
{
"flrID": "2021-07-03T07:04:00-FLR-001",
"instruments": [
{
"displayName": "GOES-P: EXIS 1.0-8.0"
}
],
"beginTime": "2021-07-03T07:04Z",
"peakTime": "2021-07-03T07:17Z",
"endTime": "2021-07-03T07:22Z",
"classType": "M2.7",
"sourceLocation": "N23W78",
"activeRegionNum": 12838,
"linkedEvents": [
{
"activityID": "2021-07-03T08:00:00-CME-001"
}
],
"link": "https://kauai.ccmc.gsfc.nasa.gov/DONKI/view/FLR/17189/-1"
},
{
"flrID": "2021-07-03T14:18:00-FLR-001",
"instruments": [
{
"displayName": "GOES-P: EXIS 1.0-8.0"
}
],
"beginTime": "2021-07-03T14:18Z",
"peakTime": "2021-07-03T14:29Z",
"endTime": "2021-07-03T14:34Z",
"classType": "X1.5",
"sourceLocation": "N23W80",
"activeRegionNum": 12838,
"linkedEvents": [
{
"activityID": "2021-07-03T14:48:00-CME-001"
}
],
"link": "https://kauai.ccmc.gsfc.nasa.gov/DONKI/view/FLR/17201/-1"
},
{
"flrID": "2021-07-03T16:59:00-FLR-001",
"instruments": [
{
"displayName": "GOES-P: EXIS 1.0-8.0"
}
],
"beginTime": "2021-07-03T16:59Z",
"peakTime": "2021-07-03T17:03Z",
"endTime": "2021-07-03T17:14Z",
"classType": "M1.0",
"sourceLocation": "N23W82",
"activeRegionNum": 12838,
"linkedEvents": [
{
"activityID": "2021-07-03T17:36:00-CME-001"
}
],
"link": "https://kauai.ccmc.gsfc.nasa.gov/DONKI/view/FLR/17208/-1"
}
]
The JSON contains an array so during deseralisation, you need to use JSONArray and then get the last element of the array using length-1,
JSONArray jsonArr = new JSONArray(jsonText);
JSONObject lastObject = (JSONObject) jsonArr.get(jsonArr.length()-1);
If length-1 does not give you the last record then you need to sort the JSONArray based on the required field and then take the first or last based on sorting. You can check the below thread for sorting the JSONArray
How can I sort a JSONArray in JAVA
I am working on one requirement , where I need to get the length of below json
and length of filters object in the entire json
attaching the json for refrence.
{
"employeeId": "41825",
"userId": "tawright",
"sourceSystem": "Visibility",
"loginId": "nudayaku",
"groupBy": "order",
"isPretty": false,
"limit": 10,
"offset": 0,
"sortBy": "so_date",
"sortOrder": "desc",
"filters": [{
"name": "type",
"value": "POS",
"op": "eq"
}],
"srpGoalHeaderId": 3069181,
"srpGoalQuotaId": 1750558,
"category": "PRD & SVC|AG",
"goalSheet": "2020 CS020 28-Jul-2019 to 25-Jul-2020",
"loggedInUser": "nudayaku",
"requestedScreen": "orderSearch.g2c"
}
Can some one help me how can i get the Length of the filters involved in this JSon.
I am getting the below error while i am parsing the JSOn
org.json.simple.JSONArray cannot be cast to org.json.JSONArray
import java.io.FileOutputStream;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.sql.Timestamp;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.json.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.cisco.export.utils.SFDbConnection;
import com.fasterxml.jackson.core.JsonParser;
//Assign value from Input Export JSon
JSONObject json = getJSONObject(getFilters());
String employeeId = (String) json.get("employeeId");
Long planId = (Long) json.get("planId");
Long srpGoalHeaderId = (Long) json.get("srpGoalHeaderId");
Long allocationId = (Long) json.get("allocationId");
String nodeName = (String) json.get("nodeName");
String erpPosFlag = (String) json.get("erpPosFlag");
Long soNumber = (Long) json.get("soNumber");
ObjectMapper mapper = new ObjectMapper();
JSONArray filters = (JSONArray) json.get("filters");
System.out.println("filters" +filters);
error log:
Exception is java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.JSONArray java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.JSONArray
Thanks,
Nikhil
You are importing JSONArray as below (from a different library)
import org.json.JSONArray;
Import it from json simple as well
import org.json.simple.JSONArray;
Hope this helps !!
try mapping it into an object and you could get the filters length pretty easily.
eg
var object = {
"employeeId": "41825",
"userId": "tawright",
"sourceSystem": "Visibility",
"loginId": "nudayaku",
"groupBy": "order",
"isPretty": false,
"limit": 10,
"offset": 0,
"sortBy": "so_date",
"sortOrder": "desc",
"filters": [{
"name": "type",
"value": "POS",
"op": "eq"
}],
"srpGoalHeaderId": 3069181,
"srpGoalQuotaId": 1750558,
"category": "PRD & SVC|AG",
"goalSheet": "2020 CS020 28-Jul-2019 to 25-Jul-2020",
"loggedInUser": "nudayaku",
"requestedScreen": "orderSearch.g2c"
}
console.log('filters length',object.filters.length);
to parse the json into object use JsonParser
e.g.
JSONParser parse = new JSONParser();
Object EmployeeObject = parse.parse(yourjsonobject);
console.log(EmployeeObject.filters.length);
The length of the "filters" jsonArray is actually 1 because within the square [] brackets you have {} which is accounted as 1 element in the array.
Assuming you want to get the size of the json object inside the json array i.e
{
"name": "type",
"value": "POS",
"op": "eq"
}
you need to iterate through the object to get the size
You're mixing two incompatible libraries of json processing, hence the error.
From your code snippet:
import org.json.JSONArray;
import org.json.simple.JSONObject;
Obviously its wrong:
org.json.simple is offered by http://code.google.com/p/json-simple/
org.json is supported by some other libraries like this one for instance
The best would be opening the corresponding classes and checking out to which jar they actually belong (for example, in IntelliJ you can simply press Alt + F1 for this).
So stick to one library and it should work.
Instead of mixing two different library operations which are wrong indeed, you can make use of the same ObjectMapper which you are creating to get the filters list
ObjectMapper mapper = new ObjectMapper();
Using the above mapper convert your json to a Map<String, Object>, and then you can have handle on the entire json. You can do something like this to get the list out of your json.
Map<String, Object> map = mapper.readValue(json, Map.class);
if(map.containsKey("filters")) {
List<Object> filters = (List) map.get("filters");
System.out.println(filters);
System.out.println(filters.size());
}
This snippets prints out:
[{name=type, value=POS, op=eq}]
1
I solve it that way
myJson ->My Json that inside it exist an array
fieldPath -> the path to the array
import com.jayway.jsonpath.JsonPath;
import org.json.JSONArray;
import org.json.simple.JSONObject;
JSONObject myJson;
public long CountFields(String fieldPath) {
String onlyMyArray=JsonPath.parse(myJson).read(fieldPath).toString();
JSONArray jsonArray = new JSONArray(onlyMyArray);
return jsonArray.length();
}
I'm a little clueless on this subject, but I'm trying to parse some Json into variables using GSON.
Here's an example of part of the Json I'm trying to parse. The goal is to get an array of objects containing the .ogg path(i.e. "minecraft/sounds/mob/stray/death2.ogg") as well as its corresponding hash variable.
{
"objects": {
"minecraft/sounds/mob/stray/death2.ogg": {
"hash": "d48940aeab2d4068bd157e6810406c882503a813",
"size": 18817
},
"minecraft/sounds/mob/husk/step4.ogg": {
"hash": "70a1c99c314a134027988106a3b61b15389d5f2f",
"size": 9398
}
}
Any help or suggestions on how to get that sort of result with GSON would be appreciated.
Xeyeler , the following is a solution that will work. A better way is to look at how GSON can convert this to an object directly. I have not tried it though
import java.util.Map;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class MainProgram {
public static void main(String[] args) {
String mineCraft = "{ \"objects\": { \"minecraft/sounds/mob/stray/death2.ogg\": { \"hash\": \"d48940aeab2d4068bd157e6810406c882503a813\", \"size\": 18817 }, \"minecraft/sounds/mob/husk/step4.ogg\": { \"hash\": \"70a1c99c314a134027988106a3b61b15389d5f2f\", \"size\": 9398 }}}";
JsonParser parser = new JsonParser();
JsonObject objects = parser.parse(mineCraft).getAsJsonObject().get("objects").getAsJsonObject();
for (Map.Entry<String, JsonElement> entry : objects.entrySet()) {
String oggFileName = entry.getKey();
JsonElement attributes = entry.getValue();
System.out.println(
"Key is " + oggFileName + " and the hash value is " + attributes.getAsJsonObject().get("hash"));
}
}
}
Actually I am trying to parse schema.org Microdata from HTML source pages using a java library.I tried many sites finally I found any23 and Mf2j from github For .apache any23 I didn't find proper documentation so I left that and finally using Mf2j I build the project and I got the jar. I created a sample project to execute Mf2j to parse Microdata. Here is the sample code which I wrote.
package org.mypro;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;
import com.kylewm.mf2j.Mf2Parser;
public class MySample {
/**
* #param args
* #throws URISyntaxException
* #throws IOException
*/
public static void main(String[] args) throws IOException, URISyntaxException {
// TODO Auto-generated method stub
Mf2Parser parser = new Mf2Parser()
.setIncludeAlternates(true)
.setIncludeRelUrls(true);
URL server = new URL("http://blogbasics.com/examples-of-blogs/");
Properties systemProperties = System.getProperties();
systemProperties.setProperty("http.proxyHost","pp.xyz.com");
systemProperties.setProperty("http.proxyPort","1234");
HttpURLConnection connection = (HttpURLConnection)server.openConnection();
connection.connect();
Map<String,Object> parsed = parser.parse(new URI("http://blogbasics.com/examples-of-blogs/"));
for (Entry<String, Object> string : parsed.entrySet()) {
System.out.println(string.getKey() +" = "+string.getValue());
}
}
}
But I got output like this. I got ly image URL's but I need all Microdata metadata.
rels = {"Icon":["http://blogbasics.com/wp-content/uploads/cropped-Blog-Basics-favicon.png"],"Shortcut":["http://blogbasics.com/wp-content/uploads/cropped-Blog-Basics-favicon.png"],"apple-touch-icon-precomposed":["http://blogbasics.com/wp-content/uploads/apple-touch-icon-144x144.png","http://blogbasics.com/wp-content/uploads/apple-touch-icon-114x114.png","http://blogbasics.com/wp-content/uploads/apple-touch-icon-72x72.png","http://blogbasics.com/wp-content/uploads/apple-touch-icon-57x57.png"],"canonical":["http://blogbasics.com/examples-of-blogs/"],"external":["http://blogbasics.com","http://allisondduncan.com","http://www.kuldipsonsjewellers.com/Earring.html","http://jin6000.tumblr.com/","http://ckckarate.com","http://www.ferrypress.com/profile.php?u=Herman1879"],"nofollow":["http://bit.ly/1EQF6HG","https://curlcentric.leadpages.net/leadbox/14581e773f72a2%3A12e927026b46dc/5758142528880640/","http://blogbasics.com/examples-of-blogs/#comment-261","http://blogbasics.com","http://blogbasics.com/examples-of-blogs/#comment-262","http://allisondduncan.com","http://blogbasics.com/examples-of-blogs/#comment-270","http://blogbasics.com/examples-of-blogs/#comment-736","http://www.kuldipsonsjewellers.com/Earring.html","http://blogbasics.com/examples-of-blogs/#comment-1407","http://blogbasics.com/examples-of-blogs/#comment-3036","http://blogbasics.com/examples-of-blogs/#comment-5682","http://blogbasics.com/examples-of-blogs/#comment-6877","http://blogbasics.com/examples-of-blogs/#comment-8615","http://jin6000.tumblr.com/","http://blogbasics.com/examples-of-blogs/#comment-8684","http://ckckarate.com","http://blogbasics.com/examples-of-blogs/#comment-18326","http://www.ferrypress.com/profile.php?u=Herman1879","http://blogbasics.com/examples-of-blogs/#comment-22883","http://blogbasics.com/examples-of-blogs/#comment-26672","http://blogbasics.com/examples-of-blogs/#respond","http://www.blogbasics.com","http://www.blogbasics.com/privacy","http://www.blogbasics.com/terms-conditions/","http://www.blogbasics.com/contact/"],"publisher":["https://www.google.com/+Blogbasics"],"stylesheet":["http://blogbasics.com/wp-content/themes/tru/style.css?v=1427736009&ver=2.1.3","http://fonts.googleapis.com/css?family=Open+Sans%3A300%2C400italic%2C400%2C600%2C700%7CRokkitt&ver=1.5","http://optinskin.com/src-4/min/normalize.min.css?ver=4.3","http://blogbasics.com/wp-content/plugins/OptinSkin/skins/1/style.css?ver=4.3","http://blogbasics.com/wp-content/themes/tru/includes/lib/spyr_slidingshare/style.css?ver=0.9.3"]}
items = []
rel-urls = {"http://allisondduncan.com":{"rels":["external","nofollow"],"text":"Allison Duncan"},"http://bit.ly/1EQF6HG":{"rels":["nofollow"]},"http://blogbasics.com":{"rels":["external","nofollow"],"text":"Paul Odtaa"},"http://blogbasics.com/examples-of-blogs/":{"rels":["canonical"]},"http://blogbasics.com/examples-of-blogs/#comment-1407":{"rels":["nofollow"],"text":"Reply"},"http://blogbasics.com/examples-of-blogs/#comment-18326":{"rels":["nofollow"],"text":"Reply"},"http://blogbasics.com/examples-of-blogs/#comment-22883":{"rels":["nofollow"],"text":"Reply"},"http://blogbasics.com/examples-of-blogs/#comment-261":{"rels":["nofollow"],"text":"Reply"},"http://blogbasics.com/examples-of-blogs/#comment-262":{"rels":["nofollow"],"text":"Reply"},"http://blogbasics.com/examples-of-blogs/#comment-26672":{"rels":["nofollow"],"text":"Reply"},"http://blogbasics.com/examples-of-blogs/#comment-270":{"rels":["nofollow"],"text":"Reply"},"http://blogbasics.com/examples-of-blogs/#comment-3036":{"rels":["nofollow"],"text":"Reply"},"http://blogbasics.com/examples-of-blogs/#comment-5682":{"rels":["nofollow"],"text":"Reply"},"http://blogbasics.com/examples-of-blogs/#comment-6877":{"rels":["nofollow"],"text":"Reply"},"http://blogbasics.com/examples-of-blogs/#comment-736":{"rels":["nofollow"],"text":"Reply"},"http://blogbasics.com/examples-of-blogs/#comment-8615":{"rels":["nofollow"],"text":"Reply"},"http://blogbasics.com/examples-of-blogs/#comment-8684":{"rels":["nofollow"],"text":"Reply"},"http://blogbasics.com/examples-of-blogs/#respond":{"rels":["nofollow"],"text":"Cancel reply"},"http://blogbasics.com/wp-content/plugins/OptinSkin/skins/1/style.css?ver=4.3":{"media":"all","rels":["stylesheet"],"type":"text/css"},"http://blogbasics.com/wp-content/themes/tru/includes/lib/spyr_slidingshare/style.css?ver=0.9.3":{"media":"all","rels":["stylesheet"],"type":"text/css"},"http://blogbasics.com/wp-content/themes/tru/style.css?v=1427736009&ver=2.1.3":{"media":"all","rels":["stylesheet"],"type":"text/css"},"http://blogbasics.com/wp-content/uploads/apple-touch-icon-114x114.png":{"rels":["apple-touch-icon-precomposed"]},"http://blogbasics.com/wp-content/uploads/apple-touch-icon-144x144.png":{"rels":["apple-touch-icon-precomposed"]},"http://blogbasics.com/wp-content/uploads/apple-touch-icon-57x57.png":{"rels":["apple-touch-icon-precomposed"]},"http://blogbasics.com/wp-content/uploads/apple-touch-icon-72x72.png":{"rels":["apple-touch-icon-precomposed"]},"http://blogbasics.com/wp-content/uploads/cropped-Blog-Basics-favicon.png":{"rels":["Shortcut","Icon"],"type":"image/x-icon"},"http://ckckarate.com":{"rels":["external","nofollow"],"text":"Ed JP"},"http://fonts.googleapis.com/css?family=Open+Sans%3A300%2C400italic%2C400%2C600%2C700%7CRokkitt&ver=1.5":{"media":"all","rels":["stylesheet"],"type":"text/css"},"http://jin6000.tumblr.com/":{"rels":["external","nofollow"],"text":"Edward Carty"},"http://optinskin.com/src-4/min/normalize.min.css?ver=4.3":{"media":"all","rels":["stylesheet"],"type":"text/css"},"http://www.blogbasics.com":{"rels":["nofollow"],"text":"Blog Basics"},"http://www.blogbasics.com/contact/":{"rels":["nofollow"],"text":"Contact"},"http://www.blogbasics.com/privacy":{"rels":["nofollow"],"text":"Privacy Policy"},"http://www.blogbasics.com/terms-conditions/":{"rels":["nofollow"],"text":"Terms and Conditions"},"http://www.ferrypress.com/profile.php?u=Herman1879":{"rels":["external","nofollow"],"text":"hgh xl"},"http://www.kuldipsonsjewellers.com/Earring.html":{"rels":["external","nofollow"],"text":"Jewellery Shop in Ranchi"},"https://curlcentric.leadpages.net/leadbox/14581e773f72a2%3A12e927026b46dc/5758142528880640/":{"rels":["nofollow"],"text":"(Click Here)"},"https://www.google.com/+Blogbasics":{"rels":["publisher"]}}
Actually I need output in this form, not in json format. I need data content like this. I found this using JavaScript Microdata Parser from http://foolip.org/microdatajs/live/ but I need same kind of parser in java. Please suggest me. Thanks
"type": [ "http://schema.org/WebPage" ], "properties": { "mainContentOfPage": [ { "type": [ "http://schema.org/Blog" ], "properties": { "blogPost": [ { "type": [ "http://schema.org/BlogPosting" ], "properties": { "headline": [ "Examples of Blogs" ], "author": [ { "type": [ "http://schema.org/Person" ], "properties": { "name": [ "Kenneth Byrd" ] } } ],
Read more: http://foolip.org/microdatajs/live/#ixzz3lJJII7g0
I am able to parse the following data into a java object:
{
"name": "testname",
"address": "1337 455 ftw",
"type": "sometype",
"notes": "cheers mate"
}
using this code:
public class Test
{
public static void main (String[] args) throws Exception
{
URL objectGet = new URL("http://10.0.0.4/file.json");
URLConnection yc = objectGet.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
Gson gson = new Gson();
try {
DataO data = new Gson().fromJson(in, DataO.class);
System.out.println(data.getName());
}catch (Exception e) {
e.printStackTrace();
}
}
}
But now I want to store a list of these objects out of the following JSON String:
[
{
"name": "testname",
"address": "1337 455 ftw",
"type": "sometype",
"notes": "cheers mate"
},
{
"name": "SumYumStuff",
"address": "no need",
"type": "clunkdroid",
"notes": "Very inefficient but high specs so no problem."
}
]
Could someone help me modify my code to do this?
You could specify the type to deserialize into as an array or as a collection.
As Array:
import java.io.FileReader;
import com.google.gson.Gson;
public class GsonFoo
{
public static void main(String[] args) throws Exception
{
Data0[] data = new Gson().fromJson(new FileReader("input.json"), Data0[].class);
System.out.println(new Gson().toJson(data));
}
}
class Data0
{
String name;
String address;
String type;
String notes;
}
As List:
import java.io.FileReader;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class GsonFoo
{
public static void main(String[] args) throws Exception
{
List<Data0> data = new Gson().fromJson(new FileReader("input.json"), new TypeToken<List<Data0>>(){}.getType());
System.out.println(new Gson().toJson(data));
}
}
A quick look in the Gson User Guide indicates that this might not be possible since the deserializer doesn't know the type of the elements since you could have elements of different types in the array.
Collections Limitations
Can serialize collection of arbitrary objects but can not deserialize
from it Because there is no way for the user to indicate the type of
the resulting object While deserializing, Collection must be of a
specific generic type