How to get a data from JSON in React Native? - json

When I run the below code in my react-native project
console.log("Response= "+JSON.stringify(response));
I can get output like below in my console.
Response= {"assets":[{"height":3888,"uri":"file:///data/user/0/com.facebook2/cache/rn_image_picker_lib_temp_6b8db334-4fcc-40ba-94a0-325191a89011.jpg","width":5184,"fileName":"rn_image_picker_lib_temp_6b8db334-4fcc-40ba-94a0-325191a89011.jpg","type":"image/jpeg","fileSize":1914937}]}
How I print the 'uri' from that JSON response ?

Looking at your data we can break it down by what we see. So, with JSON we have a Javascript Object which contains a param of assets. So to print assets we would console.log(response.assets)
Assets is an array with one item, so we want to get the first item from that which would be console.log(response.assets[0]).
Then we want the uri from that first assets object which would be console.log(response.assets[0].uri)
Hope this is what you are looking for.

You can use Dot Notation to access the properties of an object.
In your response json, it is seen that it has an array with name assets. The required property uri is inside the array. You can access it simply by
response.assets[0].uri
if there were multiple items in your assets array, you can simply loop over the array and get the values,
const length = response.assets.length;
for(let i=0; i< length; i++)
console.log('URI is = ', response.assets[i].uri)
const response = {"assets":[{"height":3888,"uri":"file:///data/user/0/com.facebook2/cache/rn_image_picker_lib_temp_6b8db334-4fcc-40ba-94a0-325191a89011.jpg","width":5184,"fileName":"rn_image_picker_lib_temp_6b8db334-4fcc-40ba-94a0-325191a89011.jpg","type":"image/jpeg","fileSize":1914937}]};
console.log('URI =', response.assets[0].uri)

Related

access JSON sub-object without knowing key value

For example, assuming I can generate this page but won't be able to hard-code the article number, how can I access the thumbnail link in this entry?
https://en.wikipedia.org/w/api.php?action=query&titles=Arcadius&prop=pageimages&format=json&origin=*&pithumbsize=100
assuming that json is a name of your json object, you can try this
var thumbnail = json["query"]["pages"]["2180"]["thumbnail"];
// or thumbnail source
var source = json["query"]["pages"]["2180"]["thumbnail"]["source"];
if you have a json string , not an object you will have to parse it at first
var json=JSON.Parse(jsonString);

How do I parse this JSON data and get the attributes I want as a list?

I am building an air app that needs to access data from Firebase Database using the REST API. I am able to get the data but I do not know how to get the information I need from that data.
Basically, I have an Android app and a Desktop app. The android app uploads/Modifies data and the Desktop app needs to access that data and send signals to the ESP8266 via a socket connection. I have a list of 'Lights' each light as a 'lightname', 'status' and 'pin'. I want to be able to loop through all the 'pin's.
When I try to get the data , I get this JSON:
{"-LAb_YKS9l7qQno25AY5":{"lightname":"light1","pin":"14","status":"on","timestamp":1524303808146},"-LAb_cRpsGpQfr7JbCfI":{"lightname":"light2","pin":"15","status":"on","timestamp":1524303830159},"-LAb_zbf2sYuyTtW_uEr":{"lightname":"blah","pin":"9","status":"on","timestamp":1524303921921},"-LAba68lzyG15n6anuSF":{"lightname":"dishl","pin":"7","status":"on","timestamp":1524303955946},"-LAdZW2JjQVGfLMc_sb4":{"lightname":"cxcxc","pin":"14","status":"on","timestamp":1524337092712}}
I want to loop through all the lights and access their 'pin' values.
Here is what i've tried:
for (var i:int = 0; i < e.target.data.length; i++)
{
trace(e.target.data[i]["status"]);
}
I get this error:
Property 0 not found on String and there is no default value.
Any help is greatly appreciated.
Because the e.target.data answer you get from Firebase is a String and first you need to JSON.parse(...) it to transform it into data. Then your data is not an Array, it is an Object. You loop through it as following:
// Parse the incoming data.
var aData:Object = JSON.parse(e.target.data);
// Loop through string keys of the data object.
for (var aKey:String in aData)
{
trace("");
trace(aKey);
// Assign entry reference to another local variable. This is not
// mandatory, but the following code is shorter and thus more readable.
var anEntry:Object = aData[aKey];
trace(anEntry['pin']);
trace(anEntry['status']);
trace(anEntry['lightname']);
}
If you have no use of key values, you might do it in a slightly different way:
// Parse the incoming data.
var aData:Object = JSON.parse(e.target.data);
// Loop through values of the data object while ignoring the keys.
for each (var anEntry:Object in aData)
{
trace("");
trace(anEntry['pin']);
trace(anEntry['status']);
trace(anEntry['lightname']);
}

Having trouble with methods for Vimeo's new API to get url of files

I try to get the the data of the field "files" with "quality" with the value "hd". I read about the endpoints and I try to make a request with thee values of this fields.
My problem is I can't obtain the value of the field "files" because is a vector. How is the way to pass this like an endpoint?.
The resource of research is the follow: https://developer.vimeo.com/api/endpoints.
Actually with my code only I can obtein the principal fields but I can't acces to the fileds compound with others.
$client_id = "XXX";
$client_secret= "XXX";
$access_token = "XXX" ;
$lib = new Vimeo($client_id, $client_secret, $access_token);
$response = $lib->request("/videos/videoID")
The return value of the request method will always be an associative array. This associative array will contain three values
body: An associative array containing the full json response
headers: An associative array of response headers
status: The HTTP status code (see http://httpstatus.es for a detailed list)
To access the files array specifically, you would use the following code:
$response['body']['files']
This will contain an array of file objects for you to loop through.
foreach ($response['body']['files'] as $file) {
// use a $file
}
NOTE: Only PRO members have access to video files, and even then only to their own files.

trying to convert data from Domino Access Service to a JSON string via JSON.stringify

I want to store the result from a call to a Domino Access Service (DAS) in a localStorage however when I try to convert the result object to a JSON string I get an error.
With DAS you get the result as an Array e.g.:
[
{
"#entryid":"1-CD90722966A36D758025725800726168",
"#noteid":"16B46",
Does anyone know how I get rid of the square brackets or convert the Array quickly to a JSON object?
Here is a snippet of my code:
var REST = "./myREST.xsp/notesView";
$.getJSON(REST,function(data){
if(localStorage){
localStorage.setItem('myCatalog',JSON.stringify(data));
}
});
Brackets are part of the JSON syntax. They indicate that this is an array of objects. And as you point to a view it is very likely that you would get more than one object back (one for each entry in the view).
So if you are only interested in the first element you could do this:
var REST = "./myREST.xsp/notesView";
$.getJSON(REST,function(data){
if(localStorage){
var firstRecord = data[0] || {};
localStorage.setItem('myCatalog',JSON.stringify(firstRecord));
}
});
Otherwise, you would need to define a loop to handle each of the objects :-)
/John

Getting data from JSON Array

my JSON response is as given below
JSON response is
{"code":201,"message":[["TEST Action","NA","30-11--2011"],["TEST Action 2","NA","30-11--2011"]]}.
i want to take the data correspond to 'message'.i used JSON Array.and got response as
JSON array response is
[["TEST Action","NA","30-11--2011"],["TEST Action 2","NA","30-11--2011"]].
Now how can i access each array in that?
You should expand on what you have done, what language you are using, etc. Normally, you should be able to index into the array with the standard notation. In python for example you can do something along the lines of json_data["message"][0] to access the first array and json_data["message"][1] to access the second.
something like :
var d = JSON.parse('{"code":201,"message":[["TEST Action","NA","30-11--2011"],["TEST Action 2","NA","30-11--2011"]]}')
and then you can access each array in message part as :
d.message.forEach(function(obj) { console.log(obj); });