In the following JSON data, I am trying to get the “image” key, but keep getting the KeyNotFoundException error when trying to access it (either through .value or .child)
{
"data": {
"id": "2ndCYJK",
"title": "c1f64245afb2",
"url_viewer": "https://ibb.co/2ndCYJK",
"url": "https://i.ibb.co/w04Prt6/c1f64245afb2.gif",
"display_url": "https://i.ibb.co/98W13PY/c1f64245afb2.gif",
"size": "42",
"time": "1552042565",
"expiration":"0",
"image": {
"filename": "c1f64245afb2.gif",
"name": "c1f64245afb2",
"mime": "image/gif",
"extension": "gif",
"url": "https://i.ibb.co/w04Prt6/c1f64245afb2.gif",
},
"thumb": {
"filename": "c1f64245afb2.gif",
"name": "c1f64245afb2",
"mime": "image/gif",
"extension": "gif",
"url": "https://i.ibb.co/2ndCYJK/c1f64245afb2.gif",
},
"medium": {
"filename": "c1f64245afb2.gif",
"name": "c1f64245afb2",
"mime": "image/gif",
"extension": "gif",
"url": "https://i.ibb.co/98W13PY/c1f64245afb2.gif",
},
"delete_url": "https://ibb.co/2ndCYJK/670a7e48ddcb85ac340c717a41047e5c"
},
"success": true,
"status": 200
}
Code?
You first have to query “data” then look for “image” inside that.
json gets passed to the method as a JSONItem parameter and seems to be valid JSON
DIM data As JSONItem = json.Value("data")
me.mID = data.Value("id")
me.mTitle = data.Value("title")
me.mURLViewer = data.Value("url_viewer")
me.mURL = data.Value("url")
me.mDisplayURL = data.Value("display_url")
me.mSize = data.Value("size")
me.mTime = data.Value("time") // what format is this in?
me.mExpiration = data.Value("expiration")
me.mDeleteURL = data.Value("delete_url")
if (json.HasKey("image")) then
DIM imageJSON As JSONItem = json.Value("image")
me.mImageFileName = imageJSON.Value("filename")
me.mImageName = imageJSON.Value("name")
me.mImageMIME = imageJSON.Value("mime")
me.mImageExtension = imageJSON.Value("extension")
me.mImageURL = imageJSON.Value("url")
end if
if (json.HasKey("thumb")) then
DIM thumbJSON As JSONItem = json.Value("thumb")
me.mThumbnailFileName = thumbJSON.Value("filename")
me.mThumbnailName = thumbJSON.Value("name")
me.mThumbnailMIME = thumbJSON.Value("mime")
me.mThumbnailExtension = thumbJSON.Value("extension")
me.mThumbnailURL = thumbJSON.Value("url")
end if
if (json.HasKey("medium")) then
DIM mediumJSON As JSONItem = json.Value("medium")
me.mMediumFileName = mediumJSON.Value("filename")
me.mMediumName = mediumJSON.Value("name")
me.mMediumMIME = mediumJSON.Value("mime")
me.mMediumExtension = mediumJSON.Value("extension")
me.mMediumURL = mediumJSON.Value("url")
end if
You need to look in data, not json.
A string “1552042565” since that is how it was coded in the original JSON (i.e., it is within quotes). You can then convert the string to something else, but it is being passed as a string.
hehe… That’s just a note to myself. I emailed ImgBB to find out what it is as trying to convert it on this site Convert Seconds & Days Since Year 0-1970 yielded incorrect or no results.
Thanks Kem, some times I miss the little things (would have never caught that)