Getting inside JSON directory? - json

JSON:
{"status":"ok","count":4,"count_total":4,"pages":1,"posts":[{"id":15,"type":"post","slug":"sg-and-t13-friends-again","url":"http:\/\/example.com.com\/instanews\/sg-and-t13-friends-again\/","status":"publish","title":"SG and T13 friends again","title_plain":"SG and T13 friends again","content":"<p>again?<\/p>\n","excerpt":"<p>again?<\/p>\n","date":"2014-10-08 20:28:52","modified":"2014-10-08 20:28:52","categories":[{"id":2,"slug":"selena-gomez","title":"Selena Gomez","description":"","parent":0,"post_count":3},{"id":3,"slug":"taylor-swift","title":"Taylor Swift","description":"","parent":0,"post_count":2}],"tags":[],"author":{"id":1,"slug":"admin","name":"admin","first_name":"","last_name":"","nickname":"admin","url":"","description":""},"comments":[],"attachments":[{"id":16,"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwQj1R1tv3z6lo1_500.png","slug":"tumblr_nc5rkwqj1r1tv3z6lo1_500","title":"tumblr_nc5rkwQj1R1tv3z6lo1_500","description":"","caption":"","parent":15,"mime_type":"image\/png","images":{"full":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwQj1R1tv3z6lo1_500.png","width":500,"height":630},"thumbnail":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwQj1R1tv3z6lo1_500-150x150.png","width":150,"height":150},"medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwQj1R1tv3z6lo1_500-238x300.png","width":238,"height":300},"large":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwQj1R1tv3z6lo1_500.png","width":500,"height":630},"vw_small":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwQj1R1tv3z6lo1_500-360x200.png","width":360,"height":200},"vw_square_small":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwQj1R1tv3z6lo1_500-360x360.png","width":360,"height":360},"vw_medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwQj1R1tv3z6lo1_500-500x420.png","width":500,"height":420},"vw_square_medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwQj1R1tv3z6lo1_500.png","width":500,"height":630},"vw_large":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwQj1R1tv3z6lo1_500.png","width":500,"height":630}}}],"comment_count":0,"comment_status":"open","thumbnail":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwQj1R1tv3z6lo1_500-150x150.png","custom_fields":{"vw_review_average_score":["0"],"vw_post_featured":["0"],"vw_post_layout":["right"],"vw_enable_review":["0"]},"thumbnail_size":"thumbnail","thumbnail_images":{"full":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwQj1R1tv3z6lo1_500.png","width":500,"height":630},"thumbnail":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwQj1R1tv3z6lo1_500-150x150.png","width":150,"height":150},"medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwQj1R1tv3z6lo1_500-238x300.png","width":238,"height":300},"large":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwQj1R1tv3z6lo1_500.png","width":500,"height":630},"vw_small":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwQj1R1tv3z6lo1_500-360x200.png","width":360,"height":200},"vw_square_small":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwQj1R1tv3z6lo1_500-360x360.png","width":360,"height":360},"vw_medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwQj1R1tv3z6lo1_500-500x420.png","width":500,"height":420},"vw_square_medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwQj1R1tv3z6lo1_500.png","width":500,"height":630},"vw_large":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_nc5rkwQj1R1tv3z6lo1_500.png","width":500,"height":630}}},{"id":9,"type":"post","slug":"selena-gomez-new-trailer","url":"http:\/\/example.com.com\/instanews\/selena-gomez-new-trailer\/","status":"publish","title":"Selena Gomez new trailer","title_plain":"Selena Gomez new trailer","content":"<p>Not really\u00a0“Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.”<\/p>\n","excerpt":"<p>Not really\u00a0“Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, …<\/p>\n","date":"2014-10-08 20:22:02","modified":"2014-10-08 20:25:19","categories":[{"id":2,"slug":"selena-gomez","title":"Selena Gomez","description":"","parent":0,"post_count":3}],"tags":[],"author":{"id":1,"slug":"admin","name":"admin","first_name":"","last_name":"","nickname":"admin","url":"","description":""},"comments":[],"attachments":[{"id":11,"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/IMG_1842.jpg","slug":"img_1842","title":"IMG_1842","description":"","caption":"","parent":9,"mime_type":"image\/jpeg","images":{"full":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/IMG_1842.jpg","width":1280,"height":1024},"thumbnail":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/IMG_1842-150x150.jpg","width":150,"height":150},"medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/IMG_1842-300x240.jpg","width":300,"height":240},"large":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/IMG_1842-1024x819.jpg","width":1024,"height":819},"vw_small":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/IMG_1842-360x200.jpg","width":360,"height":200},"vw_square_small":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/IMG_1842-360x360.jpg","width":360,"height":360},"vw_medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/IMG_1842-750x420.jpg","width":750,"height":420},"vw_square_medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/IMG_1842-750x750.jpg","width":750,"height":750},"vw_large":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/IMG_1842-1140x641.jpg","width":1140,"height":641}}}],"comment_count":0,"comment_status":"open","thumbnail":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/IMG_1842-150x150.jpg","custom_fields":{"vw_review_average_score":["0"],"vw_post_featured":["0"],"vw_post_layout":["right"],"vw_enable_review":["0"]},"thumbnail_size":"thumbnail","thumbnail_images":{"full":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/IMG_1842.jpg","width":1280,"height":1024},"thumbnail":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/IMG_1842-150x150.jpg","width":150,"height":150},"medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/IMG_1842-300x240.jpg","width":300,"height":240},"large":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/IMG_1842-1024x819.jpg","width":1024,"height":819},"vw_small":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/IMG_1842-360x200.jpg","width":360,"height":200},"vw_square_small":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/IMG_1842-360x360.jpg","width":360,"height":360},"vw_medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/IMG_1842-750x420.jpg","width":750,"height":420},"vw_square_medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/IMG_1842-750x750.jpg","width":750,"height":750},"vw_large":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/IMG_1842-1140x641.jpg","width":1140,"height":641}}},{"id":7,"type":"post","slug":"taylor-swift-new-album","url":"http:\/\/example.com.com\/instanews\/taylor-swift-new-album\/","status":"publish","title":"Taylor Swift new album","title_plain":"Taylor Swift new album","content":"<p>3 weeks!\u00a0Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.<\/p>\n<p>Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.<\/p>\n","excerpt":"<p>3 weeks!\u00a0Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, …<\/p>\n","date":"2014-10-08 20:21:20","modified":"2014-10-08 20:27:44","categories":[{"id":3,"slug":"taylor-swift","title":"Taylor Swift","description":"","parent":0,"post_count":2}],"tags":[],"author":{"id":1,"slug":"admin","name":"admin","first_name":"","last_name":"","nickname":"admin","url":"","description":""},"comments":[],"attachments":[{"id":14,"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkV1rx2xpto1_500.jpg","slug":"tumblr_ncqmsrndkv1rx2xpto1_500","title":"tumblr_ncqmsrndkV1rx2xpto1_500","description":"","caption":"","parent":7,"mime_type":"image\/jpeg","images":{"full":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkV1rx2xpto1_500.jpg","width":500,"height":648},"thumbnail":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkV1rx2xpto1_500-150x150.jpg","width":150,"height":150},"medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkV1rx2xpto1_500-231x300.jpg","width":231,"height":300},"large":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkV1rx2xpto1_500.jpg","width":500,"height":648},"vw_small":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkV1rx2xpto1_500-360x200.jpg","width":360,"height":200},"vw_square_small":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkV1rx2xpto1_500-360x360.jpg","width":360,"height":360},"vw_medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkV1rx2xpto1_500-500x420.jpg","width":500,"height":420},"vw_square_medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkV1rx2xpto1_500.jpg","width":500,"height":648},"vw_large":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkV1rx2xpto1_500-500x641.jpg","width":500,"height":641}}}],"comment_count":0,"comment_status":"open","thumbnail":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkV1rx2xpto1_500-150x150.jpg","custom_fields":{"vw_review_average_score":["0"],"vw_post_featured":["0"],"vw_post_layout":["right"],"vw_enable_review":["0"]},"thumbnail_size":"thumbnail","thumbnail_images":{"full":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkV1rx2xpto1_500.jpg","width":500,"height":648},"thumbnail":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkV1rx2xpto1_500-150x150.jpg","width":150,"height":150},"medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkV1rx2xpto1_500-231x300.jpg","width":231,"height":300},"large":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkV1rx2xpto1_500.jpg","width":500,"height":648},"vw_small":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkV1rx2xpto1_500-360x200.jpg","width":360,"height":200},"vw_square_small":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkV1rx2xpto1_500-360x360.jpg","width":360,"height":360},"vw_medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkV1rx2xpto1_500-500x420.jpg","width":500,"height":420},"vw_square_medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkV1rx2xpto1_500.jpg","width":500,"height":648},"vw_large":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/tumblr_ncqmsrndkV1rx2xpto1_500-500x641.jpg","width":500,"height":641}}},{"id":5,"type":"post","slug":"selena-gomez-reported-pregnant","url":"http:\/\/example.com.com\/instanews\/selena-gomez-reported-pregnant\/","status":"publish","title":"Selena Gomez reported pregnant","title_plain":"Selena Gomez reported pregnant","content":"<p>yolo? fake!<\/p>\n","excerpt":"<p>yolo? fake!<\/p>\n","date":"2014-10-08 20:20:17","modified":"2014-10-08 20:26:23","categories":[{"id":2,"slug":"selena-gomez","title":"Selena Gomez","description":"","parent":0,"post_count":3}],"tags":[],"author":{"id":1,"slug":"admin","name":"admin","first_name":"","last_name":"","nickname":"admin","url":"","description":""},"comments":[],"attachments":[{"id":12,"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/Selena-Gomez-Photoshoot.jpg","slug":"selena-gomez-photoshoot","title":"Selena Gomez - Photoshoot","description":"","caption":"","parent":5,"mime_type":"image\/jpeg","images":{"full":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/Selena-Gomez-Photoshoot.jpg","width":720,"height":541},"thumbnail":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/Selena-Gomez-Photoshoot-150x150.jpg","width":150,"height":150},"medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/Selena-Gomez-Photoshoot-300x225.jpg","width":300,"height":225},"large":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/Selena-Gomez-Photoshoot.jpg","width":720,"height":541},"vw_small":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/Selena-Gomez-Photoshoot-360x200.jpg","width":360,"height":200},"vw_square_small":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/Selena-Gomez-Photoshoot-360x360.jpg","width":360,"height":360},"vw_medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/Selena-Gomez-Photoshoot-720x420.jpg","width":720,"height":420},"vw_square_medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/Selena-Gomez-Photoshoot.jpg","width":720,"height":541},"vw_large":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/Selena-Gomez-Photoshoot.jpg","width":720,"height":541}}}],"comment_count":0,"comment_status":"open","thumbnail":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/Selena-Gomez-Photoshoot-150x150.jpg","custom_fields":{"vw_review_average_score":["0"],"vw_post_featured":["0"],"vw_post_layout":["right"],"vw_enable_review":["0"]},"thumbnail_size":"thumbnail","thumbnail_images":{"full":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/Selena-Gomez-Photoshoot.jpg","width":720,"height":541},"thumbnail":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/Selena-Gomez-Photoshoot-150x150.jpg","width":150,"height":150},"medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/Selena-Gomez-Photoshoot-300x225.jpg","width":300,"height":225},"large":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/Selena-Gomez-Photoshoot.jpg","width":720,"height":541},"vw_small":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/Selena-Gomez-Photoshoot-360x200.jpg","width":360,"height":200},"vw_square_small":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/Selena-Gomez-Photoshoot-360x360.jpg","width":360,"height":360},"vw_medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/Selena-Gomez-Photoshoot-720x420.jpg","width":720,"height":420},"vw_square_medium":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/Selena-Gomez-Photoshoot.jpg","width":720,"height":541},"vw_large":{"url":"http:\/\/example.com.com\/instanews\/wp-content\/uploads\/Selena-Gomez-Photoshoot.jpg","width":720,"height":541}}}],"query":{"ignore_sticky_posts":true}}
I'm trying to get the full picture from the first post. My solution (which works):
let urlPath = "http://example.com/instanews/api/get_posts/"
let url: NSURL = NSURL(string: urlPath)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url, completionHandler: {data, response, error -> Void in
println("Task completed")
if((error) != nil) {
// If there is an error in the web request, print it to the console
println(error.localizedDescription)
}
var err: NSError?
var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary
if(err != nil) {
// If there is an error parsing JSON, print it to the console
println("JSON Error \(err!.localizedDescription)")
} else {
var result: AnyObject = jsonResult["posts"]! as NSArray
var result2: AnyObject = result[0]!
var result3: AnyObject = result2["attachments"]!!
var result4: AnyObject = result3[0]!
var result5: AnyObject = result4["images"]!!
var result6: AnyObject = result5["full"]!!
var result7: AnyObject = result6["url"]!!
println(result7)
}
})
task.resume()
I'm not happy with those var result7 etc. this is obviously not the right way to do it. I'm not quite sure why can't we just use result["posts"][0] and stack it like that.
Also can you please explain why sometimes there has to be double exclamation point? I know that it is force unwrap, but why is it double?
Thanks!

You can consider to use some native swift json library that can take advantage of the Optional semantic e.g. https://github.com/owensd/json-swift
Then your code could be rewritten as
if let result7 = jsonResult["posts"]["attachments"]["images"]["full"]["url"].string {
println(result7)
}

Related

axios or fetch get local json - strange response format and Unexpected token in JSON then i tried to parse it

My axios response (or fetch, tried both) it's plain text (string). But I expect javaScript map. If I tried JSON.parse() (or from fetch .json()) I got error:
Uncaught (in promise) SyntaxError: Unexpected token in JSON at
position 0
at JSON.parse ()
at eval (List.vue?8915:66)
UTF-8 without BOM
I checked my json via few json validators online - json is valid
I tried create my json via some online json creator - the same error
so I have few questions:
I thought that json is a special format. If you got it via axios/fetch your response always will be js map? It's not true?
What is the best way to creating and validating json to be sure it's ok?
I use vue cli, there haven't json loader (i think so, not sure, it not use webpack, but I can't see some loaders). It could be problem? Or some environment settings? I tried to check it, but maybe can't find.
Or maybe ma header is still wrong, ad accept give me nothing?
votingService.ts
import axios, { AxiosResponse } from "axios";
export const getVotingsService = (): Promise<AxiosResponse> => {
return axios
.get("http://localhost:8080/fakeData/votings.json", {
headers: {
"accept": "application/json",
'Content-Type': 'application/json'
},
})
};
List.vue
...
private getVotings() {
getVotingsService().then(response => {
console.log("resp", response.data); //string, data is ok
console.log(JSON.parse(response.data)); //error
});
}
...
votings.json (in public folder)
{
"votings": [
{
"createDate": "20-07-20",
"deadline": "20-08-20",
"status": "active",
"description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
},
{
"createDate": "20-07-20",
"deadline": "20-08-20",
"status": "active",
"description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
},
{
"createDate": "20-07-20",
"deadline": "20-08-20",
"status": "active",
"description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
}
]
}
chrome consola:
chrome network response:
chrome network headers:
Problem solved. I'm not sure why, but VSCode didn't want to save me file without BOM, so I still had UTF-8 with BOM
On August 20, 2020, Axios released v0.20.0 which addresses the issue of removing the byte order marker (UTF-8 BOM) when transforming the response.

How to extract nested fields from MongoDB?

So my problem is that I have a database structure (designed by someone else and I have to work on it now) as follows:
DBS:
Database 1
Database 2
Database 3
Collection 1
Collection 2
field_1
field_1_1
field_1_1_1
field_1_2
field_2
field_3
Collection 3
Collection 4
Database 4
Now I want to extract the field field_1_1_1 any idea how I can query that?
So far I have tried applying find_one on Database 3.Collection 2.filed_1.field_1_1.field_1_1_1 but obviously it did not work.
So here goes the actual content as requested. This is what 1 item in the collection "tempStorage" under the database "workApp" looks like.
{"_id":{"tag":"i4x","org":"Temp","course":"CXV_08","category":"about","name":"overview","revision":null},
"definition":
{"data":
{"data":
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
uis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
onsequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
illum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
roident, sunt in culpa qui officia deserunt mollit anim id est laborum"
}
}
}
Edit: Uploaded the actual fields.
What does work is if I export the entire collection and then parse through it but the data is already .4 GB and I do not think that that can be the only option and that there must exist something better.
Anyone with good experience in MongoDB who can help me out?
Try this:
var data = db.tempStorage.find()
data[0].definition.data.data
Here, db.tempStorage.find() will give you all the results in an Array variable data, which you can iterate by passing an index value and using dot notation to reach out deep into the document as I have done in data[0].definition.data.data.
If you have only 1 document within collection than findOne() can also be used.
var data = db.tempStorage.findOne()
data.definition.data.data
HTH!
Thanks.

How to Achieve Just the Ctrl + A Functionality of Contenteditable Attribute?

I'm trying to display a div with some example code for users. I want them to be able to select inside the div and use Ctrl + A to select all of the example code, but I don't want them to be able to edit the actual text (lest the remove a bit on accident and then it won't work).
I'm achieving this right now through contenteditable set on the div, but how do I get the same functionality of this attribute sans the actual text editing?
EDIT: Looking at the solution linked in #Mr. Llama's comment, I can use this method to achieve the primary functionality I'm looking for. However, if possible I would like to simply allow for the Ctrl + A command my users are familiar with, and still allow for the manual highlighting of snippets of the div's text. Again, all while disallowing the addition or removal of any text.
Seems to work for me when I prevent the onkeydown, oncut and onpaste events.
for (const elm of document.getElementsByClassName('editable-not-editable')) {
elm.setAttribute('contenteditable', true);
elm.spellcheck = false;
elm.oncut = () => false;
elm.onpaste = () => false;
elm.onkeydown = (event) => {
if (event.metaKey || event.ctrlKey) {
return;
}
event.preventDefault();
}
}
<div class="editable-not-editable">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
deserunt mollit anim id est laborum.
</div>

WPF split HTML string for every TextBlock

Does anyone know how to split the HTML string for every TextBlock? I split them, but something is wrong. The lines count are not the same in every page. How do I solve this issue?
Here's the code:
XAML:
<Window x:Class="Ebook.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:WPFMitsuControls;assembly=WPFMitsuControls"
Title="eBook" Height="600" Width="800"
Loaded="MainWindow_OnLoaded" Background="Silver">
<DockPanel>
<Viewbox Margin="10">
<Grid>
<controls:Book x:Name="myBook" Width="600" Height="400" Margin="20">
<controls:Book.ItemTemplate>
<DataTemplate>
<Border BorderThickness="4" BorderBrush="Gray" Background="White">
<ContentControl Content="{Binding .}" />
</Border>
</DataTemplate>
</controls:Book.ItemTemplate>
</controls:Book>
<Button Content="<" HorizontalAlignment="Left" VerticalAlignment="Center" VerticalContentAlignment="Center" Background="Transparent" Height="50" Click="AutoPreviousClick" />
<Button Content=">" HorizontalAlignment="Right" VerticalAlignment="Center" VerticalContentAlignment="Center" Background="Transparent" Height="50" Click="AutoNextClick" />
</Grid>
</Viewbox>
</DockPanel>
</Window>
CODE:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Globalization;
using System.IO;
namespace Ebook
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
string html_str = "<p>The standard <b>Lorem Ipsum</b> passage, used since the 1500s</p>"
+ "<p>'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'</p>"
+ "<p>Section 1.10.32 of 'de Finibus Bonorum et Malorum', written by Cicero in 45 BC</p>"
+ "<p>'Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?'</p>"
+ "<p><u>1914 translation by H. Rackham</u></p>"
+ "<p>'But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?'</p>"
+ "<p>Section 1.10.33 of 'de Finibus Bonorum et Malorum', written by Cicero in 45 BC</p>"
+ "<p>'At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.'</p>"
+ "<p><i>1914 translation by H. Rackham</i></p>"
+ "<p>'On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains.'</p>";
private void AutoNextClick(object sender, RoutedEventArgs e)
{
myBook.AnimateToNextPage(false, 700);
myBook.Focus();
}
private void AutoPreviousClick(object sender, RoutedEventArgs e)
{
myBook.AnimateToPreviousPage(false, 700);
myBook.Focus();
}
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
List<string> lines = WrapText(html_str, 200, "Arial", 12);
int rows = 20;
int pages = lines.Count / rows;
string _Page = "";
int j = 0, k = 0;
TextBlock tb1;
for (int i = 0; i < pages; i++)
{
tb1 = new TextBlock();
tb1.TextWrapping = TextWrapping.Wrap;
tb1.Margin = new Thickness(5);
tb1.TextAlignment = TextAlignment.Justify;
_Page = "";
for (j = 0; j < rows; j++)
{
_Page += lines[rows * i + j] + "\n";
k++;
}
//myBook.Items.Add(new TextBlock { Text = _Page, TextWrapping = TextWrapping.Wrap, Margin = new Thickness(5), TextAlignment = TextAlignment.Justify });
tb1.Inlines.AddRange(MarkupProcessor.HTMLToWPF(_Page));
myBook.Items.Add(tb1);
}
}
static List<string> WrapText(string text, double pixels, string fontFamily, float emSize)
{
string[] originalLines = text.Split(new string[] { " " },
StringSplitOptions.None);
List<string> wrappedLines = new List<string>();
StringBuilder actualLine = new StringBuilder();
double actualWidth = 0;
foreach (var item in originalLines)
{
FormattedText formatted = new FormattedText(item,
CultureInfo.CurrentCulture,
System.Windows.FlowDirection.LeftToRight,
new Typeface(fontFamily), emSize, Brushes.Black);
actualLine.Append(item + " ");
actualWidth += formatted.Width;
if (actualWidth > pixels)
{
wrappedLines.Add(actualLine.ToString());
actualLine = new StringBuilder();
actualWidth = 0;
}
}
if (actualLine.Length > 0)
wrappedLines.Add(actualLine.ToString());
return wrappedLines;
}
}
}
http://i.stack.imgur.com/AEpsm.png
Here's the download link to the project: http://www.megafileupload.com/en/file/532650/WPF-Ebook-zip.html
You can`t control number of lines because of this:
tb1.TextWrapping = TextWrapping.Wrap;
Line count will depend on length of string. Long strings will be wrapped.

safari crashes with this simple css?

<style>
.panel { margin:0px }
.panel:after {
content: attr(data-text);
height:100px;
overflow:scroll; /* <- looks like this is the evil css */
line-height:2em;
display:block;
}
</style>
<div class="panel">i see your lips moving but all i understand is bla bla bla </div>
<div class="panel" data-text="Lorem ipsum dolor sit amet, consectetur
adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip
ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit
esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum."> yes
</div>
can someone confirm this, if you click on the scrollbar that your browser crashes with a mousehandler error?
EDITjust to be exact.. thats the error message in crashreporter log with Safari 5.0.6
WebCore::RenderLayer::scrollTo(int, int) + 714
WebCore::RenderLayer::setScrollOffset(WebCore::IntPoint const&) + 33
WebCore::ScrollableArea::setScrollOffsetFromAnimation(WebCore::IntPoint const&) + 30
WebCore::ScrollAnimator::notityPositionChanged() + 43
WebCore::ScrollAnimator::scroll(WebCore::ScrollbarOrientation, WebCore::ScrollGranularity, float, float) + 140
WebCore::ScrollAnimatorMac::scroll(WebCore::ScrollbarOrientation, WebCore::ScrollGranularity, float, float) + 300
WebCore::ScrollableArea::scroll(WebCore::ScrollDirection, WebCore::ScrollGranularity, float) + 159
WebCore::Scrollbar::autoscrollPressedPart(double) + 123
WebCore::Scrollbar::mouseDown(WebCore::PlatformMouseEvent const&) + 129
WebCore::EventHandler::passMousePressEventToScrollbar(WebCore::MouseEventWithHitTestResults&, WebCore::Scrollbar*) + 58
WebCore::EventHandler::handleMousePressEvent(WebCore::PlatformMouseEvent const&) + 1424
WebCore::EventHandler::mouseDown(NSEvent*) + 385
-[WebHTMLView mouseDown:] + 475
WebCore::EventHandler::passMouseDownEventToWidget(WebCore::Widget*) + 388
WebCore::EventHandler::passWidgetMouseDownEventToWidget(WebCore::RenderWidget*) + 27
WebCore::EventHandler::passSubframeEventToSubframe(WebCore::MouseEventWithHitTestResults&, WebCore::Frame*, WebCore::HitTestResult*) + 720
WebCore::EventHandler::passMousePressEventToSubframe(WebCore::MouseEventWithHitTestResults&, WebCore::Frame*) + 56
WebCore::EventHandler::handleMousePressEvent(WebCore::PlatformMouseEvent const&) + 462
WebCore::EventHandler::mouseDown(NSEvent*) + 385
-[WebHTMLView mouseDown:] + 475
-[NSWindow sendEvent:] + 5381
0x1000 + 259380
0x1000 + 259265
-[NSApplication sendEvent:] + 2939
0x1000 + 223168
-[NSApplication run] + 867
NSApplicationMain + 574
0x1000 + 40174
This seems to be slightly fixed in Safari 6. The content is able to scroll on 6.0.3 on my Mac, however I cannot interact with the scrollbar using multi-touch. This means if scrollbars are set to auto-hide in System Preferences, then the content cannot scroll at all. Either way, it does not cause Safari 6 to crash.
Unfortunately, Safari 6 is not available for Windows, and the last version that is available on Windows, 5.1.7, is affected by this bug on both platforms. It does not completely crash the browser, though — it simply forces all tabs to reload.