Not able to fetch Weather Data from the Json Data using Angular - angularjs

Is the parsing way wrong? I'm trying to get weather data
function Hello($scope, $http) {
$http
.jsonp('http://api.worldweatheronline.com/free/v1/weather.ashx?q=London&format=json&num_of_days=5&key=MYKEY&callback=JSON_CALLBACK')
.success(function(data) {
var datais = JSON.stringify(data);
console.log("Datais::"+datais);
console.log("Weather::"+datais["weather"]);
})
.error(function(data){
alert("Error");
});
}
Output:
Datais::{
"data": {
"current_condition": [{
"cloudcover": "25",
"humidity": "76",
"observation_time": "09:13 AM",
"precipMM": "0.0",
"pressure": "992",
"temp_C": "9",
"temp_F": "48",
"visibility": "10",
"weatherCode": "113",
"weatherDesc": [{
"value": "Sunny"
}],
"weatherIconUrl": [{
"value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
}],
"winddir16Point": "SSW",
"winddirDegree": "200",
"windspeedKmph": "13",
"windspeedMiles": "8"
}],
"request": [{
"query": "London, United Kingdom",
"type": "City"
}],
"weather": [{
"date": "2014-01-16",
"precipMM": "1.6",
"tempMaxC": "11",
"tempMaxF": "52",
"tempMinC": "5",
"tempMinF": "41",
"weatherCode": "116",
"weatherDesc": [{
"value": "Partly Cloudy"
}],
"weatherIconUrl": [{
"value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png"
}],
"winddir16Point": "S",
"winddirDegree": "191",
"winddirection": "S",
"windspeedKmph": "30",
"windspeedMiles": "19"
}, {
"date": "2014-01-17",
"precipMM": "1.3",
"tempMaxC": "10",
"tempMaxF": "50",
"tempMinC": "5",
"tempMinF": "42",
"weatherCode": "116",
"weatherDesc": [{
"value": "Partly Cloudy"
}],
"weatherIconUrl": [{
"value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png"
}],
"winddir16Point": "SSW",
"winddirDegree": "202",
"winddirection": "SSW",
"windspeedKmph": "27",
"windspeedMiles": "17"
}, {
"date": "2014-01-18",
"precipMM": "2.7",
"tempMaxC": "10",
"tempMaxF": "49",
"tempMinC": "4",
"tempMinF": "39",
"weatherCode": "266",
"weatherDesc": [{
"value": "Light drizzle"
}],
"weatherIconUrl": [{
"value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0017_cloudy_with_light_rain.png"
}],
"winddir16Point": "S",
"winddirDegree": "177",
"winddirection": "S",
"windspeedKmph": "23",
"windspeedMiles": "15"
}, {
"date": "2014-01-19",
"precipMM": "1.0",
"tempMaxC": "8",
"tempMaxF": "46",
"tempMinC": "5",
"tempMinF": "41",
"weatherCode": "122",
"weatherDesc": [{
"value": "Overcast"
}],
"weatherIconUrl": [{
"value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png"
}],
"winddir16Point": "ESE",
"winddirDegree": "110",
"winddirection": "ESE",
"windspeedKmph": "13",
"windspeedMiles": "8"
}, {
"date": "2014-01-20",
"precipMM": "0.4",
"tempMaxC": "8",
"tempMaxF": "47",
"tempMinC": "1",
"tempMinF": "34",
"weatherCode": "116",
"weatherDesc": [{
"value": "Partly Cloudy"
}],
"weatherIconUrl": [{
"value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png"
}],
"winddir16Point": "NW",
"winddirDegree": "311",
"winddirection": "NW",
"windspeedKmph": "12",
"windspeedMiles": "7"
}]
}
}
Weather::undefined

Here your datais variable is assigned the data.
if you inspect properly the json data returned as you can see in your console.log(datais);
you will be able to access weather property in datais.data.weather.
try this
console.log("Weather::"+datais.data.weather);
You will be able to access weather property.
Further weather property is an array which has weather values from today up to 4 upcoming days. you can access them in a loop
for(i=0; i<datais.data.weather.length; i++){
console.log(datais.data.weather[i]);
}
Or for example access todays weather data:
console.log(datais.data.weather[0].date);
console.log(datais.data.weather[0].tempMaxC);
console.log(datais.data.weather[0].tempMinC);

i htink you need to access the weather this way:
console.log("Weather::"+datais.data.weather);
you may use an onlne json viewer to view the structure of your data: for example: http://jsonviewer.stack.hu/ or take at look at the browser console. firebug, for example, can show the json data as tree.
And you don't need JSON.stringify. Angular does this for you:
.success(function(data) {
console.log(data.data.weather);
})

Related

How to find a object in array React native

I have array=
[{
"2022-12-06": [{
"student": "10",
"duration": "00:00:07",
"intervals": "1"
}]
},
{
"2022-09-08": [{
"student": "20",
"duration": "00:00:07",
"intervals": "1"
},
{
"student": "300",
"duration": "00:00:07",
"intervals": "1"
},
{
"student": "10",
"duration": "00:00:07",
"intervals": "1"
}
]
},
{
"date20221208": [{
"student": "10",
"duration": "00:00:07",
"intervals": "1"
},
{
"student": "10",
"duration": "00:00:07",
"intervals": "1"
},
{
"student": "10",
"duration": "00:00:07",
"intervals": "1"
},
{
"student": "10",
"duration": "00:00:07",
"intervals": "1"
},
{
"student": "10",
"duration": "00:00:07",
"intervals": "1"
}
]
}
]
I want find a date and return value date is exits or not
for eg.
i find 2022-12-06 if array have this value then true if not then false
I want find a date and return value date is exits or not
for eg.
i find 2022-12-06 if array have this value then true if not then false
First of all this question is not about react-native. This is about basic javascript.
You can use Array.Prototype.find() method.
const arr = [{
"2022-12-06": [{
"student": "10",
"duration": "00:00:07",
"intervals": "1"
}]
},
{
"2022-09-08": [{
"student": "20",
"duration": "00:00:07",
"intervals": "1"
},
{
"student": "300",
"duration": "00:00:07",
"intervals": "1"
},
{
"student": "10",
"duration": "00:00:07",
"intervals": "1"
}
]
},
{
"date20221208": [{
"student": "10",
"duration": "00:00:07",
"intervals": "1"
},
{
"student": "10",
"duration": "00:00:07",
"intervals": "1"
},
{
"student": "10",
"duration": "00:00:07",
"intervals": "1"
},
{
"student": "10",
"duration": "00:00:07",
"intervals": "1"
},
{
"student": "10",
"duration": "00:00:07",
"intervals": "1"
}
]
}
]
const checkArray = (str) => {
const x = arr.find(item => item.hasOwnProperty(str))
return x ? true:false
// or return object.If has not return undefined
// return x
}
console.log(checkArray("2022-12-06"))
you can use the Array.prototype.find() method to search for an object in an array. This method returns the first element in the array that satisfies the provided testing function.
For example, if you have an array of objects and you want to find an object with a specific date property, you could use the find() method like this:
const array = [{date: '2022-12-06', name: 'John'}, {date: '2020-02-05', name: 'Jane'}, {date: '1990-04-07', name: 'Bob'}];
const object = array.find(obj => obj.date === '2022-12-06');
console.log(object); // {date: '2022-12-06', name: 'John'}

working with select options and an object in angularjs

I need to display category value in a dropdown using select options from this object:
[{
"_id": "57b4508923a10cd83c79a301",
"created_by": "1",
"category": "Criminal law",
"__v": 0,
"delete_status": "0",
"active_status": "1",
"modified_date": "1471434889599",
"created_date": "1471434889599"
}, {
"_id": "57b466b34f94fc982a0d926d",
"created_by": "1",
"category": "Business Law",
"__v": 0,
"delete_status": "0",
"active_status": "1",
"modified_date": "1471440563159",
"created_date": "1471440563158"
}, {
"_id": "57c6be3ae74cdc1d6a9b2224",
"created_by": "1",
"category": "Corporate Law",
"__v": 0,
"delete_status": "0",
"active_status": "1",
"modified_date": "1472642618768",
"created_date": "1472642618768"
}]
I used below code but got undefined:
<select ng-model="category" ng-options="users.id as users.category for item in items"></select>
Can any one help me?
Try this
var app = angular.module('app', []).controller('countryCtrl', ['$scope',
function($scope) {
$scope.records = [{
"_id": "57b4508923a10cd83c79a301",
"created_by": "1",
"category": "Criminal law",
"__v": 0,
"delete_status": "0",
"active_status": "1",
"modified_date": "1471434889599",
"created_date": "1471434889599"
}, {
"_id": "57b466b34f94fc982a0d926d",
"created_by": "1",
"category": "Business Law",
"__v": 0,
"delete_status": "0",
"active_status": "1",
"modified_date": "1471440563159",
"created_date": "1471440563158"
}, {
"_id": "57c6be3ae74cdc1d6a9b2224",
"created_by": "1",
"category": "Corporate Law",
"__v": 0,
"delete_status": "0",
"active_status": "1",
"modified_date": "1472642618768",
"created_date": "1472642618768"
}];
}
])
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="countryCtrl">
<select ng-init="text._id = records[0]._id" ng-model="text" ng-options="record.category for record in records track by record._id">
</select>
</div>
<select ng-model="category" ng-options="item.id as item.category for item in items"></select>
just update this in your template (y) :)

Link wont work with FusionCharts LinkedChart

I am trying to make a simple drill down that breaks down months into weeks, and am just trying to get the first column to work. I get the chart to render, but when I click on January it doesn't go to the linked chart. The external links on february and march work so I'm guessing it is a problem with the JSON data.
define(['app'], function (app) {
app.register.controller('MilesovertimeController', [function () {
var vm = this;
vm.myDataSource ={
"chart": {
"caption": "Miles Over Time",
"subCaption": "Track the miles you have run over your entire running career!",
"xAxisName": "Time (Month)",
"yAxisName": "Miles",
"theme": "fint"
},
"data": [
{
"label": "January",
"value": "10",
"link": "newchart-json-Jan"
},
{
"label": "February",
"value": "20",
"link": "n-http://fusioncharts.com"
},
{
"label": "March",
"value": "5",
"link": "n-http://fusioncharts.com"
},
{
"label": "April",
"value": "55",
"link": "n-http://fusioncharts.com"
},
{
"label": "May",
"value": "40",
"link": "n-http://fusioncharts.com"
},
{
"label": "June",
"value": "110",
"link": "n-http://fusioncharts.com"
},
{
"label": "July",
"value": "75",
"link": "n-http://fusioncharts.com"
},
{
"label": "August",
"value": "90",
"link": "n-http://fusioncharts.com"
},
{
"label": "September",
"value": "10",
"link": "n-http://fusioncharts.com"
},
{
"label": "October",
"value": "45",
"link": "n-http://fusioncharts.com"
},
{
"label": "November",
"value": "30",
"link": "n-http://fusioncharts.com"
},
{
"label": "December",
"value": "100",
"link": "n-http://fusioncharts.com"
}
],
"linkeddata": [
{
"id": "Jan",
"linkedchart": {
"chart": {
"caption": "Miles In January",
"subcaption": "This is what you did in January",
"xAxisName": "Weeks",
"yAxisName": "Miles"
},
"data": [
{
"label": "Week 1",
"value": "50"
},
{
"label": "Week 2",
"value": "25"
},
{
"label": "Week 3",
"value": "40"
},
{
"label": "Week 4",
"value": "10"
}
]
}
}
]
}
}]);
);
edit: This is what I'm trying to emulate http://www.fusioncharts.com/features/linkedcharts-for-drill-down/
IMPORTANT EDIT: When I click the january bar I get this error:
Uncaught RuntimeException: #03091456 chartobject-2.render() Error >> Unable to find the container DOM element.
<div class="container" ng-controller="MilesovertimeController as vm">
<div fusionCharts
width="1000"
height="550"
type="column2d"
datasource="{{vm.myDataSource}}">
</div>
</div>
In order to get drill-down work make sure the directive has id
var app = angular.module('myApp', ["ng-fusioncharts"]);
app.controller('MilesovertimeController', [function () {
var vm = this;
vm.myDataSource ={
"chart": {
"caption": "Miles Over Time",
"subCaption": "Track the miles you have run over your entire running career!",
"xAxisName": "Time (Month)",
"yAxisName": "Miles",
"theme": "fint"
},
"data": [
{
"label": "January",
"value": "10",
"link": "newchart-json-Jan"
},
{
"label": "February",
"value": "20",
"link": "n-http://fusioncharts.com"
},
{
"label": "March",
"value": "5",
"link": "n-http://fusioncharts.com"
},
{
"label": "April",
"value": "55",
"link": "n-http://fusioncharts.com"
},
{
"label": "May",
"value": "40",
"link": "n-http://fusioncharts.com"
},
{
"label": "June",
"value": "110",
"link": "n-http://fusioncharts.com"
},
{
"label": "July",
"value": "75",
"link": "n-http://fusioncharts.com"
},
{
"label": "August",
"value": "90",
"link": "n-http://fusioncharts.com"
},
{
"label": "September",
"value": "10",
"link": "n-http://fusioncharts.com"
},
{
"label": "October",
"value": "45",
"link": "n-http://fusioncharts.com"
},
{
"label": "November",
"value": "30",
"link": "n-http://fusioncharts.com"
},
{
"label": "December",
"value": "100",
"link": "n-http://fusioncharts.com"
}
],
"linkeddata": [
{
"id": "Jan",
"linkedchart": {
"chart": {
"caption": "Miles In January",
"subcaption": "This is what you did in January",
"xAxisName": "Weeks",
"yAxisName": "Miles"
},
"data": [
{
"label": "Week 1",
"value": "50"
},
{
"label": "Week 2",
"value": "25"
},
{
"label": "Week 3",
"value": "40"
},
{
"label": "Week 4",
"value": "10"
}
]
}
}
]
}
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="http://static.fusioncharts.com/code/latest/fusioncharts.js"></script>
<script src="http://fusioncharts.github.io/angular-fusioncharts/demos/js/angular-fusioncharts.min.js"></script>
<div ng-app="myApp">
<div class="container" ng-controller="MilesovertimeController as vm">
<div fusionCharts
width="1000"
height="550"
id="test"
type="column2d"
datasource="{{vm.myDataSource}}">
</div>
</div>
</div>

How can I use ng-repeat to render this JSON data?

How to repeat the below JSON without write for loop using only ng-repeat
$scope.cart = {
"product": [
{
"HH_STYLENUM": "2204-RYP",
"SIZE_QTY": [
{ "QTY": "11", "SIZE": "XS" },
{ "QTY": "11", "SIZE": "XL" },
{ "QTY": "111", "SIZE": "S" },
{ "QTY": "111", "SIZE": "M" },
{ "QTY": "111", "SIZE": "L" }
],
"HH_CUSTID": "2919",
"HH_ADDCART_UNITPRICE": "11.5",
"HH_ADDCART_NO": "188",
"HH_ADDCART_DATE": "2014-12-10 00:25:07.0",
"HH_ADDCART_STATUS": "CART",
"HH_COLORID": "15991"
},
{
"HH_STYLENUM": "2204-HHSH",
"SIZE_QTY": [
{ "QTY": "01", "SIZE": "XS" },
{ "QTY": "03", "SIZE": "XL" },
{ "QTY": "104", "SIZE": "S" },
{ "QTY": "51", "SIZE": "M" },
{ "QTY": "31", "SIZE": "L" }
],
"HH_CUSTID": "2919",
"HH_ADDCART_UNITPRICE": "11.5",
"HH_ADDCART_NO": "188",
"HH_ADDCART_DATE": "2014-12-10 00:25:07.0",
"HH_ADDCART_STATUS": "CART",
"HH_COLORID": "15991"
}
]
};
Based on your comments... you could use two ng-repeats. One to loop over cart.product and within that ng-repeat, another ng-repeat to loop over SIZE_QTY
<div ng-repeat="product in cart.product">
<p ng-repeat="size in product.SIZE_QTY">{{size.SIZE}}</p>

Json reader rootProperty call in sencha touch store

{
"success": true,
"message": "Successfully retrieved data for report #1",
"content": {
"fields": ["name", "2013", "2012", "2011"],
"data": [{
"name": "1",
"2011": 378.4,
"2012": 208.95,
"2013": 229.92
}, {
"name": "2",
"2011": 258.92,
"2013": 265.92
}, {
"name": "3",
"2011": 242,
"2012": 227.98,
"2013": 558.13
}, {
"name": "4",
"2011": 322.5,
"2012": 390,
"2013": 282.09
}, {
"name": "5",
"2011": 563.88,
"2013": 461.1
}, {
"name": "7",
"2011": 268.5,
"2012": 305.48
}]
}
}
this is my json .. I want to read data:[{name:"",..... }] from my store,so I am giving rootProperty: content[0].data but it is not working, can anyone please tell me how to read that data and help me on this issue. Thank you .
Found it rootProperty: 'content.data' working fine.My issue is with url . Thank you

Resources