This is the structure i made to decode the data i get from JSON
This the main code where i call the array result and tap into the tile and print out the title.
So my question is that result is array and it contain 14 items and in that 14 item there are 10 additional thing and title is one of them, so i want to print the title using FOR loop .
This the JSON file i am trying to parse
One way you could iterate over each of your items is using the for X in Y pattern.
for movie in decodeData.results {
print(movie.title)
print(movie.poster_path)
}
This pattern will iterate through each result in your array and assign it to the variable name you specify in the for statement.
You can learn more here: Swift Control Flow
Related
I'm using OpenRefine to pull in information on publisher policies using the Sherpa Romeo API (Sherpa Romeo is a site that aggregates publisher policies). I've got that.
Now I need to parse the returned JSON so that those with certain pieces of information remain. The results I'm interested in need to include the following:
'any_website',
'any_repository',
'institutional_repository',
'non_commercial_institutional_repository',
'non_commercial_repository'
These pieces on information all fall under an array called "permitted_oa". For some reason, I can't even work out how to just pull out that array. I've tried writing grel expressions such as
value.parseJson().items.permitted_oa
but it never reutrns anything.
I wish I could share the JSON but it's too big.
I can see a couple of issues here.
Firstly the Sherpa API response items is an array (i.e. a list of things). When you have an array in the JSON, you either have to select a particular item from the array, or you have to explicitly work through the list of things in the array (aka iterate across the array) in your GREL. If you've previously worked with arrays in GREL you'll be familiar with this, but if you haven't
value.parseJson().items[0] -> first item in the array
value.parseJson().items[1] -> second item in the array
value.parseJson().items[2] -> third item in the array etc. etc.
If you know there is only ever going to be a single item in the array then you can safely use value.parseJson().items[0]
However, if you don't know how many items will be in the array and you are interested in them all, you will have to iterate over the array using a GREL control such as "forEach":
forEach(value.parseJson().items, v, v)
is a way of iterating over the array - each time the GREL finds an item in the array, it will assign it to a variable "v" and then you can do a further operation on that value using "v" as you would usually use "value" (see https://docs.openrefine.org/manual/grel#foreache1-v-e2 for an example of using forEach on an array)
Another possibility is to use join on the array. This will join all the things in an array into a string.
value.parseJson().items.join("|")
It looks like the Sherpa JSON uses Arrays liberally so you may find more arrays you have to deal with to get to the values you want.
Secondly, in the JSON you pasted "oa_permitted" isn't directly in the "item" but in another array called "publisher_policy" - so you'll need to navigate that as well. So:
value.parseJson().items[0].publisher_policy[0].permitted_oa[0]
would get you the first permitted_oa object in the first publisher_policy in the first item in the items array. If you wanted to (for example) get a list of locations from the JSON you have pasted you could use:
value.parseJson().items[0].publisher_policy[0].permitted_oa[0].location.location.join("|")
Which will give you a pipe ("|") separated list of locations based on the assumption there is only a single item, single publisher_policy and singe permitted_oa - which is true in the case of the JSON you've supplied here (but might not always be true)
I am developing an app in Android using Google sheets as DB and my resulting JSON object is of the format:
{"range":"Sheet1!A1:B3","majorDimension":"ROWS","values":[["question 1","answer1"],["question 2","answer2"],["question 3","answer3"]]}
I extract the JSON array from it and get:
[["question 1","answer1"],["question 2","answer2"],["question 3","answer3"]]
Now I want to separate the questions and save them into separate arrays, also answers into another array. Basically I want to break/parse each element of the JSON array, however I am only able to get the whole element and not the first value of each element.
Since I don't have any search criteria here how can I get the first/second value of each element separately?
You can try this below:
json_data = [["question 1","answer1"],["question 2","answer2"],["question 3","answer3"]]
questions = [i[0] for i in json_data]
answers = [i[1] for i in json_data]
I'm having trouble getting a specific set of values from an array of arrays because the values come up as aundefineda. my array is stored in state and set up like this:
survey: [{names: ["sara", "tom"]}, {age: ["17", "33"]}, {col: ["blue", "green"]}]
To access just the names, I've been trying to use the line:
console.log(this.state.survey.names)
When I check, the console has "undefined" for the names. When I check the value of survey, it shows up as an array of arrays, where each nested array has two values stored inside.
I want to check this because I am trying to map out the nested arrays and mapping out Survey names isn't working because the length shows 0. I am successful in mapping out nested arrays when they are not stored in state, so I am wondering what I am missing or if my syntax is incorrect.
You're trying to access an array as if it was an object, instead of an array of objects. A simple fix is -
this.state.survey.find(item => item.names).names
In the long term, unless you have a reason to store this as an array, it would be a lot easier to reorganize and store as objects. If you restructure this to be something like
survey: {
names: ['name1', 'name2'],
age: ...etc
If you are trying to say that Sara is 17 and her col is blue, why not just store that as a single object?
I'm trying to retrieve Firebase data with Polymer Fire. When I look in the console it's returning two objects, however the length of the array is three. When I'm trying to execute a dom-repeat I'm successfully printing two filled in rows but also one empty row. How is this possible?
Firebase stores data as associative arrays, essentially a dictionary of key/value pairs.
That means that in order to deal with arrays, it converts an array to a dictionary when you store it and then back to an actual array when you read it. Here you are getting bitten by the SDK converting your non-array into an array by padding it with a leading element.
If you don't want the SDK to do this conversion, the easiest way is to store the items with a non-numeric key, e.g. "item1", "item2".
Read more about how Firebase deals with arrays in this classic blog post: https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html
I am trying to use an array in NetLogo to store my images and call the values using their index. Looks like I am getting stuck with common manner of accessing the array's value via arrayName[0].
How do I do that in NetLogo? googling doesn't seem to have the answer.
My array:
let imgArray ["easy1.png" "easy2.png" "easy3.png" "easy4.png" "easy5.png"]
I am trying to fix the image in the following manner:
clear-drawing import-drawing imgArray[1]
You're looking for item:
item 1 imgArray
Note that it's zero-indexed, so the first item is item 0 imgArray, though first imgArray is more idiomatic.
Also, arrays in NetLogo are called lists.