Related
I'm trying to read date from JSON using a URL , but it throws an exception. and the response from header is 301 Moved Permanently
this basically means the there is something wrong with link, but I cannot figure out what it is exactly since the API provider did not mention anything in regard to changes to their API
Any help is appreciated.
here is my code
private static String readUrl(String urlString) throws Exception {
BufferedReader reader = null;
try {
URL url = new URL(urlString);
reader = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuffer buffer = new StringBuffer();
int read;
char[] chars = new char[1024];
while ((read = reader.read(chars)) != -1)
buffer.append(chars, 0, read);
return buffer.toString();
} finally {
if (reader != null)
reader.close();
}
}
String url = "http://webservice.fanart.tv/v3/movies/" + movie.id + "?api_key=" + apikey;
List<String> enClearLogos = new ArrayList<>();
try {
JSONObject json = new JSONObject(readUrl(url));
JSONArray jsonArray = json.getJSONArray("hdmovielogo");
for(int i = 0; i < jsonArray.length(); i++){
JSONObject movieObject = jsonArray.getJSONObject(i);
if (movieObject.getString("lang").equalsIgnoreCase("en"))
enClearLogos.add(movieObject.getString("url"));
}
} catch (Exception e) {
e.printStackTrace();
}
here example
{
"name": "The Bad Guys",
"tmdb_id": "629542",
"imdb_id": "tt8115900",
"hdmovieclearart": [
{
"id": "362695",
"url": "https://assets.fanart.tv/fanart/movies/629542/hdmovieclearart/the-bad-guys-6231fd4379a87.png",
"lang": "en",
"likes": "6"
},
{
"id": "362696",
"url": "https://assets.fanart.tv/fanart/movies/629542/hdmovieclearart/the-bad-guys-6231fd53d3a96.png",
"lang": "ru",
"likes": "3"
},
{
"id": "367992",
"url": "https://assets.fanart.tv/fanart/movies/629542/hdmovieclearart/the-bad-guys-6280fb431126b.png",
"lang": "cz",
"likes": "2"
}
],
"hdmovielogo": [
{
"id": "369396",
"url": "https://assets.fanart.tv/fanart/movies/629542/hdmovielogo/the-bad-guys-629dc8e4da93a.png",
"lang": "fr",
"likes": "3"
},
{
"id": "368375",
"url": "https://assets.fanart.tv/fanart/movies/629542/hdmovielogo/the-bad-guys-62899cfba2c00.png",
"lang": "en",
"likes": "3"
},
{
"id": "371110",
"url": "https://assets.fanart.tv/fanart/movies/629542/hdmovielogo/the-bad-guys-62bdec1d38353.png",
"lang": "es",
"likes": "2"
},
{
"id": "355025",
"url": "https://assets.fanart.tv/fanart/movies/629542/hdmovielogo/the-bad-guys-61ba168a41d9b.png",
"lang": "ru",
"likes": "2"
},
{
"id": "355031",
"url": "https://assets.fanart.tv/fanart/movies/629542/hdmovielogo/the-bad-guys-61ba1f851cd32.png",
"lang": "en",
"likes": "2"
},
{
"id": "355032",
"url": "https://assets.fanart.tv/fanart/movies/629542/hdmovielogo/the-bad-guys-61ba1f96a497e.png",
"lang": "en",
"likes": "1"
},
{
"id": "366828",
"url": "https://assets.fanart.tv/fanart/movies/629542/hdmovielogo/the-bad-guys-626984d807f4d.png",
"lang": "cz",
"likes": "1"
},
{
"id": "360922",
"url": "https://assets.fanart.tv/fanart/movies/629542/hdmovielogo/the-bad-guys-6217a7c5ba60d.png",
"lang": "it",
"likes": "0"
}
],
"movieposter": [
{
"id": "367357",
"url": "https://assets.fanart.tv/fanart/movies/629542/movieposter/the-bad-guys-6274ea20dcc86.jpg",
"lang": "en",
"likes": "2"
},
{
"id": "371114",
"url": "https://assets.fanart.tv/fanart/movies/629542/movieposter/the-bad-guys-62bdecfbd6715.jpg",
"lang": "es",
"likes": "2"
},
{
"id": "368000",
"url": "https://assets.fanart.tv/fanart/movies/629542/movieposter/the-bad-guys-628108a944560.jpg",
"lang": "pt",
"likes": "2"
},
{
"id": "367358",
"url": "https://assets.fanart.tv/fanart/movies/629542/movieposter/the-bad-guys-6274ea33e2ed5.jpg",
"lang": "00",
"likes": "2"
},
{
"id": "355026",
"url": "https://assets.fanart.tv/fanart/movies/629542/movieposter/the-bad-guys-61ba169a2b57e.jpg",
"lang": "en",
"likes": "2"
},
{
"id": "358210",
"url": "https://assets.fanart.tv/fanart/movies/629542/movieposter/the-bad-guys-61ee6f9a14706.jpg",
"lang": "00",
"likes": "1"
},
{
"id": "367991",
"url": "https://assets.fanart.tv/fanart/movies/629542/movieposter/the-bad-guys-6280fb200c5bf.jpg",
"lang": "cz",
"likes": "0"
},
{
"id": "367990",
"url": "https://assets.fanart.tv/fanart/movies/629542/movieposter/the-bad-guys-6280fb1ba6d39.jpg",
"lang": "cz",
"likes": "0"
}
],
"moviethumb": [
{
"id": "371117",
"url": "https://assets.fanart.tv/fanart/movies/629542/moviethumb/the-bad-guys-62bdee5332ad2.jpg",
"lang": "es",
"likes": "2"
},
{
"id": "367994",
"url": "https://assets.fanart.tv/fanart/movies/629542/moviethumb/the-bad-guys-6280fb7b224fd.jpg",
"lang": "cz",
"likes": "1"
},
{
"id": "370972",
"url": "https://assets.fanart.tv/fanart/movies/629542/moviethumb/the-bad-guys-62bbbc9385121.jpg",
"lang": "en",
"likes": "0"
},
{
"id": "367260",
"url": "https://assets.fanart.tv/fanart/movies/629542/moviethumb/the-bad-guys-6272bfb851ca5.jpg",
"lang": "en",
"likes": "0"
}
],
"moviebackground": [
{
"id": "355027",
"url": "https://assets.fanart.tv/fanart/movies/629542/moviebackground/the-bad-guys-61ba16ab7df92.jpg",
"lang": "",
"likes": "2"
},
{
"id": "367177",
"url": "https://assets.fanart.tv/fanart/movies/629542/moviebackground/the-bad-guys-6271f6e99b835.jpg",
"lang": "",
"likes": "0"
}
],
"moviedisc": [
{
"id": "367993",
"url": "https://assets.fanart.tv/fanart/movies/629542/moviedisc/the-bad-guys-6280fb602b271.png",
"lang": "cz",
"likes": "1",
"disc": "1",
"disc_type": "bluray"
},
{
"id": "358213",
"url": "https://assets.fanart.tv/fanart/movies/629542/moviedisc/the-bad-guys-61ee85436574f.png",
"lang": "en",
"likes": "0",
"disc": "1",
"disc_type": "bluray"
}
],
"moviebanner": [
{
"id": "358216",
"url": "https://assets.fanart.tv/fanart/movies/629542/moviebanner/the-bad-guys-61ee93348cb62.jpg",
"lang": "en",
"likes": "1"
},
{
"id": "370973",
"url": "https://assets.fanart.tv/fanart/movies/629542/moviebanner/the-bad-guys-62bbbd7215d83.jpg",
"lang": "en",
"likes": "0"
}
]
}
exception message
W/System.err: org.json.JSONException: Value <html> of type java.lang.String cannot be converted to JSONObject
W/System.err: at org.json.JSON.typeMismatch(JSON.java:112)
W/System.err: at org.json.JSONObject.<init>(JSONObject.java:168)
W/System.err: at org.json.JSONObject.<init>(JSONObject.java:181)
The http:// causes url redirection and throws this message in the header response (301 Moved Permanently) so I had to use https:// instead now everything works nicely.
more info here Response 301 Moved Permanently
I would like to know how to unset or destroy a level of JSON object.
Is this can be done using any Java Spring framework or could be define any custom functionality for this solution.
But require to retain the data inside array of inside of "txtFrameArr" as below.Example i have this result :
[
{
"txtFrameArr": {
"CEPDuplicateTextFrame1": [
{
"marked": 1,
"id_elems": [
{
"PS": "1",
"OS": "1",
"LS": "999",
"name": "MARKETING_CLAIM",
"IS": "1",
"id": "CE-MARKETING_CLAIM-1_999_1_1"
}
],
"mapCordinates": [
{
"top": 468.645102009805,
"left": 1053.47265625,
"width": 30.8357511971699,
"height": 132.963461384805
},
{
"top": 513.29412308266,
"left": 1067.05751323149,
"width": 91.1260246588899,
"height": 233.24627152016
}
],
"duplicate": 1,
"content": "baby-dry pants",
"txtframe_name": "ceptxtduplicateTF1-threaded1"
}
]
}
},
{
"txtFrameArr": {
"CEPTextFrame2": [
{
"marked": 1,
"id_elems": [
{
"PS": "1",
"OS": "1",
"LS": "2",
"name": "NET_CONTENT_STATEMENT",
"IS": "1",
"id": "CE-NET_CONTENT_STATEMENT-1_2_1_1"
}
],
"mapCordinates": [
{
"top": 22.599609375,
"left": 740.23886611805,
"width": 22.9008135909007,
"height": 33.8291015625
},
{
"top": -6.76171875,
"left": 744.522124684196,
"width": 15.4768987533043,
"height": 9.2734375
},
{
"top": -14.33203125,
"left": 740.448785213874,
"width": 22.2500000017299,
"height": 6.88811848711339
}
],
"duplicate": 0,
"content": "4단계 9-14 kg",
"txtframe_name": "ceptxtCE-NET_CONTENT_STATEMENT-1_2_1_1-threaded2"
}
]
}
},
{
"txtFrameArr": {
"CEPTextFrame3": [
{
"marked": 1,
"id_elems": [
{
"PS": "1",
"OS": "1",
"LS": "2",
"name": "USAGE_INSTRUCTIONS",
"IS": "4",
"id": "CE-USAGE_INSTRUCTIONS-4_2_1_1"
}
],
"mapCordinates": [
{
"top": 215.9384765625,
"left": -639.0234375,
"width": 29.1552734375,
"height": 55.2314453125
}
],
"duplicate": 0,
"content": "위로 당기며 입혀줍니다",
"txtframe_name": "ceptxtCE-USAGE_INSTRUCTIONS-4_2_1_1-3"
}
]
}
}
]
Supposed to be wanted like this by removing "txtFrameArr"
[
{
"CEPDuplicateTextFrame1": [
{
"marked": 1,
"id_elems": [
{
"PS": "1",
"OS": "1",
"LS": "999",
"name": "MARKETING_CLAIM",
"IS": "1",
"id": "CE-MARKETING_CLAIM-1_999_1_1"
}
],
"mapCordinates": [
{
"top": 468.645102009805,
"left": 1053.47265625,
"width": 30.8357511971699,
"height": 132.963461384805
},
{
"top": 513.29412308266,
"left": 1067.05751323149,
"width": 91.1260246588899,
"height": 233.24627152016
}
],
"duplicate": 1,
"content": "baby-dry pants",
"txtframe_name": "ceptxtduplicateTF1-threaded1"
}
]
},
{
"CEPTextFrame2": [
{
"marked": 1,
"id_elems": [
{
"PS": "1",
"OS": "1",
"LS": "2",
"name": "NET_CONTENT_STATEMENT",
"IS": "1",
"id": "CE-NET_CONTENT_STATEMENT-1_2_1_1"
}
],
"mapCordinates": [
{
"top": 22.599609375,
"left": 740.23886611805,
"width": 22.9008135909007,
"height": 33.8291015625
},
{
"top": -6.76171875,
"left": 744.522124684196,
"width": 15.4768987533043,
"height": 9.2734375
},
{
"top": -14.33203125,
"left": 740.448785213874,
"width": 22.2500000017299,
"height": 6.88811848711339
}
],
"duplicate": 0,
"content": "4단계 9-14 kg",
"txtframe_name": "ceptxtCE-NET_CONTENT_STATEMENT-1_2_1_1-threaded2"
}
]
},
{
"CEPTextFrame3": [
{
"marked": 1,
"id_elems": [
{
"PS": "1",
"OS": "1",
"LS": "2",
"name": "USAGE_INSTRUCTIONS",
"IS": "4",
"id": "CE-USAGE_INSTRUCTIONS-4_2_1_1"
}
],
"mapCordinates": [
{
"top": 215.9384765625,
"left": -639.0234375,
"width": 29.1552734375,
"height": 55.2314453125
}
],
"duplicate": 0,
"content": "위로 당기며 입혀줍니다",
"txtframe_name": "ceptxtCE-USAGE_INSTRUCTIONS-4_2_1_1-3"
}
]
}
]
Supposed to be wanted like this by removing "txtFrameArr"
I need to remove {"txtFrameArr"} but the key,value needs to be retain
Is there any specific function in Java Spring for this solution?
Supposed to be wanted like this by removing "txtFrameArr"
Is there any specific function in Java Spring for this solution?
If I get correctly what you mean - you could use JSON Path:
get all values of your objects txtFrameArr key.
Create new json array with extracted values:
For first step - you could use com.jayway.jsonpath:json-path:*, for second step - either GSON or Jackson
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
Code will look like this:
List<Map<String, Object>> frames = JsonPath.parse(json).read("$[*].txtFrameArr");
String newJson = new Gson().toJson(frames);
System.out.println("object: " + newJson);
I'm trying to insert an object to a nested array through the java api, but I get the following error
MapperParsingException[object mapping for [X] tried to parse field [null] as object, but found a concrete value]
doing it through Kibana as shown below the same script works.
Any ideas on how to fix this?
The java code is the following
HashMap<String, Object> params = new HashMap<>();
params.put("object", objectAsString);
Script script = new Script(ScriptType.INLINE, "painless", "ctx._source.media.add(params.object)", params);
UpdateResponse result = elasticClient.prepareUpdate(indexName, "Type", documentId).setScript(script).execute().actionGet();
Trhough Kibana
POST index/document/id/_update
{
"script": {
"lang": "painless",
"inline": "ctx._source.media.add(params.object)",
"params": {
"object": {
"description" : "A second image",
"height" : 5,
"weight": 5,
"name" : "Test",
"orientation" : "Vertical",
"url" : "htttp://newurl.jpg",
"tags":["first","second"],
"type":"image"
}
}
}
}
The template mapping is the following:
"mappings": {
"FOLDER_MODULE": {
"properties": {
"name": {
"type": "keyword"
},
"publisherId": {
"type": "keyword"
},
"username": {
"type": "keyword"
},
"media": {
"type": "nested",
"properties": {
"id": {
"type": "text"
},
"name": {
"type": "text"
},
"publisherId": {
"type": "short"
},
"tags": {
"type": "text"
},
"description": {
"type": "text"
},
"url": {
"type": "keyword"
},
"createdDate": {
"format": "strict_date_optional_time||epoch_millis",
"type": "date"
},
"height": {
"type": "float"
},
"width": {
"type": "float"
},
"weight": {
"type": "float"
},
"orientation": {
"type": "keyword"
},
"status": {
"type": "keyword"
},
"type": {
"type": "keyword"
},
"username": {
"type": "keyword"
}
}
}
}
}
}
Good afternoon dear Stack-overflowees!
I've got the following youtube response for a query for videos from a HTTPRequest:
null{ "kind": "youtube#searchListResponse", "etag": "\"5g01s4- S2b4VpScndqCYc5Y-8k/MnX_l4A0-CoCz7AqTuwYbZZ8Eos\"", "nextPageToken": "CAoQAA","regionCode": "NL", "pageInfo": { "totalResults": 1000000, "resultsPerPage": 10 }, "items": [ { "kind": "youtube#searchResult", "etag": "\"5g01s4-wS2b4VpScndqCYc5Y-8k/3x0aJRM9h33YMFvqksUj6alhFqU\"", "id": { "kind": "youtube#video", "videoId": "_nws2egRR1w" }, "snippet": { "publishedAt": "2015-06-26T19:00:00.000Z", "channelId": "UCY30JRSgfhYXA6i6xX1erWg", "title": "GODS IN REAL LIFE", "description": "Thank you to Smite for sponsoring this video! SMITE is an online battleground where the Gods of Old wage war in the name of conquest, glory, and most of all, ...", "thumbnails": { "default": { "url": "https://i.ytimg.com/vi/_nws2egRR1w/default.jpg", "width": 120, "height": 90 }, "medium": { "url": "https://i.ytimg.com/vi/_nws2egRR1w/mqdefault.jpg", "width": 320, "height": 180 }, "high": { "url": "https://i.ytimg.com/vi/_nws2egRR1w/hqdefault.jpg", "width": 480, "height": 360 } }, "channelTitle": "Smosh", "liveBroadcastContent": "none" } }, { "kind": "youtube#searchResult", "etag": "\"5g01s4-wS2b4VpScndqCYc5Y-8k/K_ZgSkXsMidzJqa-m1IoLJU6UBE\"", "id": { "kind": "youtube#video", "videoId": "UiPZFPYhmzs" }, "snippet": { "publishedAt": "2014-03-22T18:02:28.000Z", "channelId": "UCo_IB5145EVNcf8hw1Kku7w", "title": "Game Theory: Are SMITE's Goddesses TOO SEXY?", "description": "Become a Theorist! ▻ http://.com/mrd2ntg SMITE is a MOBA with fast-paced action, terrific game modes, and well-endowed goddesses. In fact, the ...", "thumbnails": { "default": { "url": "https://i.ytimg.com/vi/UiPZFPYhmzs/default.jpg", "width": 120, "height": 90 }, "medium": { "url": "https://i.ytimg.com/vi/UiPZFPYhmzs/mqdefault.jpg", "width": 320, "height": 180 }, "high": { "url": "https://i.ytimg.com/vi/UiPZFPYhmzs/hqdefault.jpg", "width": 480, "height": 360 } }, "channelTitle": "The Game Theorists", "liveBroadcastContent": "none" } }, { "kind": "youtube#searchResult", "etag": "\"5g01s4-wS2b4VpScndqCYc5Y-8k/wITeN0CQk1zT1C9k6Pm4BpvN3Jk\"", "id": { "kind": "youtube#video", "videoId": "5mVYnfJS73U" }, "snippet": { "publishedAt": "2016-01-05T14:00:01.000Z", "channelId": "UCLB_2GSFGa4Unb5fjq49SOw", "title": "SMITE Cinematic Trailer - 'To Hell & Back'", "description": "Welcome to SMITE, the online Battleground of the Gods. Play free at www.SmiteGame.com and seize victory in intense battles of strategic action, each fueled by ...", "thumbnails": { "default": { "url": "https://i.ytimg.com/vi/5mVYnfJS73U/default.jpg", "width": 120, "height": 90 }, "medium": { "url": "https://i.ytimg.com/vi/5mVYnfJS73U/mqdefault.jpg", "width": 320, "height": 180 }, "high": { "url": "https://i.ytimg.com/vi/5mVYnfJS73U/hqdefault.jpg", "width": 480, "height": 360 } }, "channelTitle": "SMITE by Hi-Rez Studios", "liveBroadcastContent": "none" } }, { "kind": "youtube#searchResult", "etag": "\"5g01s4-wS2b4VpScndqCYc5Y-8k/e5NJ3_X-R_m5PEqxSbN26lIAsJo\"", "id": { "kind": "youtube#video", "videoId": "Chu1XqxoQio" }, "snippet": { "publishedAt": "2015-07-16T17:03:55.000Z", "channelId": "UCWeg2Pkate69NFdBeuRFTAw", "title": "LE DIEU DE LA LOOSE ! (Smite)", "description": "On a pas l'habitude de jouer à ce genre de jeux, mais ça change un peu ! Par contre on se fait défoncer ça ça changera jamais :') Les règles : 10 Dieux ...", "thumbnails": { "default": { "url": "https://i.ytimg.com/vi/Chu1XqxoQio/default.jpg", "width": 120, "height": 90 }, "medium": { "url": "https://i.ytimg.com/vi/Chu1XqxoQio/mqdefault.jpg", "width": 320, "height": 180 }, "high": { "url": "https://i.ytimg.com/vi/Chu1XqxoQio/hqdefault.jpg", "width": 480, "height": 360 } }, "channelTitle": "SQUEEZIE", "liveBroadcastContent": "none" } }, { "kind": "youtube#searchResult", "etag": "\"5g01s4-wS2b4VpScndqCYc5Y-8k/j2Z6NrL6kZ7BqCqLC9T-iDIwVMo\"", "id": { "kind": "youtube#video", "videoId": "vAnVb_oBF7I" }, "snippet": { "publishedAt": "2014-06-07T19:00:02.000Z", "channelId": "UC-lHJZR3Gqxm24_Vd_AJ5Yw", "title": "3 HEROES, 3 QUESTIONS! - Smite: Tournament", "description": "Will they get good? Will Cry find love? Will they get a kill? Find out here!: http://ly/1p1xH05 http://youtu.be/lrXsGQO6Hrs I edited the first part of this video, Cry ...", "thumbnails": { "default": { "url": "https://i.ytimg.com/vi/vAnVb_oBF7I/default.jpg", "width": 120, "height": 90 }, "medium": { "url": "https://i.ytimg.com/vi/vAnVb_oBF7I/mqdefault.jpg", "width": 320, "height": 180 }, "high": { "url": "https://i.ytimg.com/vi/vAnVb_oBF7I/hqdefault.jpg", "width": 480, "height": 360 } }, "channelTitle": "PewDiePie", "liveBroadcastContent": "none" } }, { "kind": "youtube#searchResult", "etag": "\"5g01s4-wS2b4VpScndqCYc5Y-8k/9_S9Q9n9KHeFGuPeBIlxzFHp8hc\"", "id": { "kind": "youtube#video", "videoId": "CSFibeWqXaM" }, "snippet": { "publishedAt": "2015-08-02T16:01:42.000Z", "channelId": "UCLB_2GSFGa4Unb5fjq49SOw", "title": "SCL Spring Split Week 1 Day 4", "description": "", "thumbnails": { "default": { "url": "https://i.ytimg.com/vi/CSFibeWqXaM/default_live.jpg", "width": 120, "height": 90 }, "medium": { "url": "https://i.ytimg.com/vi/CSFibeWqXaM/mqdefault_live.jpg", "width": 320, "height": 180 }, "high": { "url": "https://i.ytimg.com/vi/CSFibeWqXaM/hqdefault_live.jpg", "width": 480, "height": 360 } }, "channelTitle": "SMITE by Hi-Rez Studios", "liveBroadcastContent": "live" } }, { "kind": "youtube#searchResult", "etag": "\"5g01s4-wS2b4VpScndqCYc5Y-8k/_HsRcEb5SgSrO7KDYGVcWnM-IyE\"", "id": { "kind": "youtube#video", "videoId": "4I7kDcRdM2Q" }, "snippet": { "publishedAt": "2012-06-02T07:31:19.000Z", "channelId": "UCTy_oLoGaQnV7LeiOkxx3qw", "title": "SMITE Gameplay (Part 1) - First Look HD", "description": "http://mmohuts.com/preview/smite for SMITE reviews, videos, screenshots and more. SMITE is a third person MOBA published by Hi-Rez Studios . MMOHuts has ...", "thumbnails": { "default": { "url": "https://i.ytimg.com/vi/4I7kDcRdM2Q/default.jpg", "width": 120, "height": 90 }, "medium": { "url": "https://i.ytimg.com/vi/4I7kDcRdM2Q/mqdefault.jpg", "width": 320, "height": 180 }, "high": { "url": "https://i.ytimg.com/vi/4I7kDcRdM2Q/hqdefault.jpg", "width": 480, "height": 360 } }, "channelTitle": "MMOHuts", "liveBroadcastContent": "none" } }, { "kind": "youtube#searchResult", "etag": "\"5g01s4-wS2b4VpScndqCYc5Y-8k/NFvQY8IZQap1sIr8ovsni7HsLAI\"", "id": { "kind": "youtube#video", "videoId": "OwAC1VD1608" }, "snippet": { "publishedAt": "2015-04-12T23:00:00.000Z", "channelId": "UCda7Fl9Q4wmqDqASRDqrVbA", "title": "COCIELO TESTE, O CRACUDO - DetonaTHOR Smite [2/2]", "description": "BAIXE E JOGUE SMITE DE GRAÇA: http://lup.vc/JogueSmite_Muca PARTE 1 DO EVENTO: https://youtu.be/DHtOrB63DD0 Instagram: ...", "thumbnails": { "default": { "url": "https://i.ytimg.com/vi/OwAC1VD1608/default.jpg", "width": 120, "height": 90 }, "medium": { "url": "https://i.ytimg.com/vi/OwAC1VD1608/mqdefault.jpg", "width": 320, "height": 180 }, "high": { "url": "https://i.ytimg.com/vi/OwAC1VD1608/hqdefault.jpg", "width": 480, "height": 360 } }, "channelTitle": "Muca Muriçoca", "liveBroadcastContent": "none" } }, { "kind": "youtube#searchResult", "etag": "\"5g01s4-wS2b4VpScndqCYc5Y-8k/_QFud2t95zRYczOqcudKnUHL4r8\"", "id": { "kind": "youtube#video", "videoId": "6q0uAevwQ2Q"
Only the first part is actually relevent, seeing as how the response starts off with a 'null'.
Now i've tried using the following code to parse the json using GSON.(result being the aforementioned JSON as a String.
JsonElement jelement = new JsonParser().parse(result);
JsonObject jobject = jelement.getAsJsonObject();
JsonArray jarray = jobject.getAsJsonArray("items");
However, Gson cannot work around the 'null' at the front of the json string. It converts the entire object in the first line not into a JsonElement but JsonNull.
Is there any way to try and get the array called 'items' from the string directly?
I've tried replacing the null with another word, i've tried several other things such as GsonBuilder.serializeNulls() and several other stackoverflow entried, yet I cannot work around this.
Any help/tips would be very much appreciated.
--edit--
following java code is used to call the api:
private String baseUrl = "https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=10&order=viewCount&q=";
String output;
String result = "";
URL url = new URL(this.baseUrl + q + "&type=video&key=" + key);
Gson gson = new Gson();
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-Type", "application/json");
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
result += output;
}
System.out.println(result);
I would guess your'e result is initiated with null so "result+=output" will be null"output" cause a NullPointer results in "null" in "+" string concatenation. Think thats your problem. Either init it with empty String or check in your while loop for null.
I'm trying to retrieve the "Maxspeed" string from the JSON below.
It iterates through the array, but when it reaches the "tag" object and tries to retrieve the "maxspeed" value, it returns an exception(below).
Would anyone know why this is happening?
Any help would be appreciated.
Thanks.
Exception:
org.json.JSONException: No value for maxspeed
Java:
JSONObject parentObject = new JSONObject(result);
JSONArray speedJSON = parentObject.getJSONArray("elements");
for (int i = 0; i < speedJSON.length(); i++) {
JSONObject element = (JSONObject) speedJSON.get(i);
if (!element.isNull("tags")) {
//JSONObject tags = (JSONObject) speedJSON.getJSONObject(i).get("tags");
String maxspeed = element.getString("maxspeed");
txtSpeed.setText(maxspeed+" here");
} else {
//Your error handling here...
}
}
//txtSpeed.setText(""+);
this.progressDialog.dismiss();
JSON:
{
"version": 0.6,
"generator": "Overpass API",
"osm3s": {
"timestamp_osm_base": "2015-03-16T20:10:02Z",
"copyright": "The data included in this document is from www.openstreetmap.org. The data is made available under ODbL."
},
"elements": [
{
"type": "node",
"id": 768053039,
"lat": 54.9526671,
"lon": -7.7273348
},
{
"type": "node",
"id": 768053040,
"lat": 54.9498094,
"lon": -7.7176056
},
{
"type": "node",
"id": 768053041,
"lat": 54.9497066,
"lon": -7.7173174
},
{
"type": "node",
"id": 768053043,
"lat": 54.9495658,
"lon": -7.7170937
},
{
"type": "node",
"id": 768053044,
"lat": 54.9495035,
"lon": -7.7169816
},
{
"type": "node",
"id": 791492493,
"lat": 54.9494183,
"lon": -7.7168205
},
{
"type": "node",
"id": 795319854,
"lat": 54.9510427,
"lon": -7.7218262
},
{
"type": "node",
"id": 795320324,
"lat": 54.9509153,
"lon": -7.7213706
},
{
"type": "node",
"id": 1922546572,
"lat": 54.9502165,
"lon": -7.7190169
},
{
"type": "node",
"id": 1922546679,
"lat": 54.9504739,
"lon": -7.7199078
},
{
"type": "node",
"id": 1922546692,
"lat": 54.9500860,
"lon": -7.7185174
},
{
"type": "node",
"id": 1922602861,
"lat": 54.9517250,
"lon": -7.7241644
},
{
"type": "node",
"id": 1922622063,
"lat": 54.9514357,
"lon": -7.7231690
},
{
"type": "node",
"id": 2673934802,
"lat": 54.9498543,
"lon": -7.7177617
},
{
"type": "way",
"id": 64273241,
"nodes": [
768053039,
1922602861,
1922622063,
795319854,
795320324
],
"tags": {
"highway": "secondary",
"maxspeed": "60",
"name": "Port Road",
"oneway": "no",
"ref": "R229"
}
},
{
"type": "way",
"id": 64887990,
"nodes": [
795320324,
1922546679,
1922546572,
1922546692,
2673934802,
768053040,
768053041,
768053043,
768053044,
791492493
],
"tags": {
"highway": "secondary",
"maxspeed": "60",
"name": "Port Road",
"oneway": "no",
"ref": "R229"
}
}
]
}
You are trying to get maxspeed from the element, but it's not there. It is inside tags object in the element.
You need something like this
if (!element.isNull("tags")) {
JSONObject tags = element.getJSONObject("tags");
String maxspeed = tags.getString("maxspeed");
txtSpeed.setText(maxspeed+" here");
} else {
//Your error handling here...
}
String maxspeed = element.getString("maxspeed");
to
String maxspeed = element.getJSONObject("tags").getString("maxspeed");