Is there a way to consistently exctract city names from strings that look like this:
{"id":25,"title":"Buenos Aires"}
{"id":26,"title":"Chicago"}
I was thinking about starting from 3 rd character from the end and then stopping at second quotation mark but I didnt find the way to do it.
asked Dec 16, 2018 at 20:43
user1743703
1312 silver badges11 bronze badges
-
Is this extracting from a json file or a JavaScript file?Lloyd Nicholson– Lloyd Nicholson2018年12月16日 20:47:21 +00:00Commented Dec 16, 2018 at 20:47
-
Simple JSON parsing : w3schools.com/jsref/jsref_parse_json.aspAdarsh– Adarsh2018年12月16日 20:48:07 +00:00Commented Dec 16, 2018 at 20:48
-
thanks, that was easyuser1743703– user17437032018年12月16日 20:48:28 +00:00Commented Dec 16, 2018 at 20:48
-
@user1743703 look at Eriks answer below then.Lloyd Nicholson– Lloyd Nicholson2018年12月16日 20:49:34 +00:00Commented Dec 16, 2018 at 20:49
1 Answer 1
The data is JSON formatted, you can decode it to js objects directly
var a = JSON.parse('{"id":25,"title":"Buenos Aires"}'),
b = JSON.parse('{"id":26,"title":"Chicago"}');
console.log(a.title); // prints Buenos Aires
console.log(b.title); // prints Chicago
answered Dec 16, 2018 at 20:47
Eriks Klotins
4,1701 gold badge14 silver badges27 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js