iv'e been trying for few days to get parameters from specific json structure with no success so far. I have a NSDictionary that get json data (i need the mToken and data from mHeader for example)
{
"$id" = 1;
mHeaders = (
{
"$id" = 4;
mPoints = 0;
mRealLeagueId = 57172a6e2276fe28bcf0d91c;
mTeamId = 57172a762276fe28bcf0e053;
mTeamLogo = avatar;
mTeamName = "Hungry Animals";
}
);
mToken = "5k8ziTBn0G5Gozs7qz68LCLBfLSgymOcLwRshMax1Q5pZi4bx6hbEOFgupoXrBNNGzsWosLs6KPsK6cG1kk/9o5778Y7JEKfo3CAPXS7qAg=";
mUser = {
"$id" = 2;
mAge = 42;
mCreateDate = "2016-04-20T07:06:30.507Z";
mEmail = "email0#gmail.com";
mFirstName = Alesandro;
mGender = 1;
mId = 57172a762276fe28bcf0e06d;
mLastLogin = "2016-04-21T07:20:40.402Z";
mLastName = Zohar;
mLogo = avatar;
mNick = "Big Boss 0";
mRegion = Global;
mTeams = (
{
"$id" = 3;
mRealLeagueId = 57172a6e2276fe28bcf0d91c;
mTeamId = 57172a762276fe28bcf0e053;
}
);
mTokens = 9644998;
};
}
So you have a dictionary after parsing the json. You need mToken which in root level of the json data, you can simply get the value by
jsonDict["mToken"]
Your mHeader is an array of dictionary so for getting the value, Do like this
for dict in jsonDict["mHeaders"] as! Array<NSDictionary> {
print(dict["mRealLeagueId"]) //prints 57172a6e2276fe28bcf0d91c
print(dict["mTeamId"]) //57172a762276fe28bcf0e053
}
Hope this helps
Related
I'm constructing some objects to store the required information in Terraform
I just defined a variable and its value as below
vnetsettings = {
HUBVNET = {
VNET_Name = "co-vnet-01"
VNET_Location = "eastasia"
VNET_Resource_Group = "CoreInfra"
VNET_Address_Space = ["10.1.0.0/16","10.2.0.0/16"]
VNET_Tags = {
env = "prod"
application = "hub"
}
VNET_DNS_Servers = ["10.1.0.4","10.2.0.4"]
}
MGMTVNET = {
VNET_Name = "mgmt-vnet-01"
VNET_Location = "eastasia"
VNET_Resource_Group = "MGMT"
VNET_Address_Space = ["10.3.0.0/16","10.4.0.0/16"]
VNET_Tags = {
env = "prod"
application = "MGMT"
}
VNET_DNS_Servers = ["10.1.0.4","10.2.0.4"]
}
}
my question is how can i bulk replace some of the attributes in the object, like VNET_Resource_Group
below is the result i want, everything same as the one above, except for the VNET_Resource_Group
vnetsettings = {
HUBVNET = {
VNET_Name = "co-vnet-01"
VNET_Location = "eastasia"
VNET_Resource_Group = "replacedvalue"
VNET_Address_Space = ["10.1.0.0/16","10.2.0.0/16"]
VNET_Tags = {
env = "prod"
application = "hub"
}
VNET_DNS_Servers = ["10.1.0.4","10.2.0.4"]
}
MGMTVNET = {
VNET_Name = "mgmt-vnet-01"
VNET_Location = "eastasia"
VNET_Resource_Group = "replacedvalue"
VNET_Address_Space = ["10.3.0.0/16","10.4.0.0/16"]
VNET_Tags = {
env = "prod"
application = "MGMT"
}
VNET_DNS_Servers = ["10.1.0.4","10.2.0.4"]
}
}
What you can do is to create a local variable which is essentially a copy of the original object. Also, while making the copy you can replace an attribute from the original objects using the merge function.
locals {
vnetsettings_updated = {
for key, value in var.vnetsettings : key => merge(value, { VNET_Resource_Group = "replacedvalue" })
}
}
# Example usage of the updated object
output "vnetsettings" {
description = "VNET settings with updated VNET_Resource_Group"
value = local.vnetsettings_updated
}
How can I convert the response message that returned from using bloomberg API in (python3 and flask) in to JSON
here is a response example:
ReferenceDataResponse = {
securityData[] = {
securityData = {
security = "FB EQUITY"
eidData[] = {
}
fieldExceptions[] = {
}
sequenceNumber = 0
fieldData = {
PX_LAST = 186.270000
VOLUME = 16746904.000000
}
}
securityData = {
security = "IBM EQUITY"
eidData[] = {
}
fieldExceptions[] = {
}
sequenceNumber = 1
fieldData = {
PX_LAST = 134.400000
VOLUME = 2551009.000000
}
}
}
}
dealing with it with the comming piece of code :
if str(msg.messageType()) == "ReferenceDataResponse":
securities = msg.getElement('securityData')
securities_count = securities.numValues()
for i in range(securities_count):
security = securities.getValueAsElement(i)
ticker = security.getElementAsString('security')
if (security.hasElement('fieldData')):
fields = security.getElement('fieldData')
fields_count = fields.numElements()
for j in range (fields_count):
security_dict = None
field = fields.getElement(j)
f_name = field.name()
f_value = field.getValueAsString()
security_dict = {"ticker":ticker ,"f_name":f_name , "f_value":f_value}
bloom_data.append(security_dict)
give me (Object of type Name is not JSON serializable)
now, I cant not access the name object to reach the name of the fields
any help will be very appreciated
After a lot of search I found this doc that is very helpful for as a schema for using the bloomberg api for dealing with the response ....
Here's the link ==> api schema
example for handeling respnse using python3:
bloom_data = []
if str(msg.messageType()) == "ReferenceDataResponse":
securities = msg.getElement('securityData')
securities_count = securities.numValues()
for i in range(securities_count):
security = securities.getValueAsElement(i)
ticker = security.getElementAsString('security')
if (security.hasElement('fieldData')):
fields = security.getElement('fieldData')
fields_count = fields.numElements()
for j in range (fields_count):
security_dict = None
field = fields.getElement(j)
f_name = field.name()
f_value = field.getValueAsString()
security_dict = {"ticker":ticker ,"f_name":str(f_name) , "f_value":f_value}
bloom_data.append(security_dict)
This is my response ... My dictnary name is "msg"
{
Result = {
Data = {
engine = {
service = {
data = (
{
checked = 1;
field = 1;
name = Ettersyn;
},
{
checked = 1;
field = 1;
name = "Med Alpha VP";
},
{
checked = 0;
field = 0;
name = "Med B.IMP.";
}
);
"engine_service_id" = 1;
"engine_service_name" = "4 Cyl";
};
type = {
"engine_id" = 1;
"engine_name" = Benzin;
};
};
tact = {
service = {
data = (
{
checked = 0;
field = 1;
name = "Little ettersyn";
},
{
checked = 0;
field = 1;
name = "Short ettersyn";
}
);
"tact_service_id" = 2;
"tact_service_name" = "21-50 hk";
};
type = {
"tact_id" = 1;
"tact_name" = "2 Takt";
};
};
};
status = success;
};
}
I try to to store the data like this
var arrayMain : NSMutableArray = msg.valueForKey("Result")?.valueForKey("Data") as NSMutableArray
My problem is i got "EXC_BAD_ACCESS" error While running ... Am also check allocation of array ... I try to store it in NSMutableDictionary its work perfectly ... How can i store this response in NSMutableArray ?
If you look carefully at the response data structure you'll see the problem.
This first operation: msg.valueForKey("Result") would return an dictionary with two key/values; a "Data" dictionary, and "Status" string.
Your second operation .valueForKey("Data") would return a dictionary with two key/values; an "Engine and "Tact" dictionary.
Because it's a dictionary, you can create a dictionary from it, which is what you said worked, or you're going to have to rethink how you want to store if if indeed you want it in an array, but you can't create an array directly from a dictionary - they are different data structures.
You cannot assign Dictionary object to Array, that's why it is giving the error.
But you can store that dictionary to array as:
var arrayMain : NSMutableArray = NSMutableArray()
arrayMain.addObject(msg.valueForKey("Result")?.valueForKey("Data"))
I am getting the following data in JSON format by the Facebook graph API. However it looks like it is in invalid JSON format. The JSON formatter says that the error is in line 1. What is this error ?
{
data = (
{
application = {
id = 2409997254;
name = Likes;
};
"created_time" = "2013-05-05T07:51:41+0000";
from = {
id = 100000347121257;
name = "Rishab Gulati";
};
id = "notif_746853089_182660043";
link = "http://www.facebook.com/photo.php?fbid=10151457234013090&set=a.427064503089.223857.746853089&type=1";
object = "<null>";
title = "Rishab Gulati, Deepika Gurnani and 2 other people like your photo.";
to = {
id = 746853089;
name = "Ashish Agarwal";
};
unread = 1;
"updated_time" = "2013-05-05T10:48:33+0000";
}
);
paging = {
next = "https://graph.facebook.com/746853089/notifications?format=json&access_token=**xxxxxxxxxx";
previous = "https://graph.facebook.com/746853089/notifications?format=json&access_token=**SNIP**&limit=5000&since=1367740301&__paging_token=notif_746853089_182660043&__previous=1";
};
summary = {
"unseen_count" = 1;
"updated_time" = "2013-05-05T10:48:33+0000";
};
}
This data is incorrect and isn't what Facebook supplies
{
"data": [
{
Where as your JSON above shows
{
data = (
{
There is a ( when there should be a {
Also consider not giving your access token out in public.
I am using this method to get data
private void getNews(int cat_id, int page)
{
this.progress.Visibility = Visibility.Visible;
var m = new SharpGIS.GZipWebClient();
Microsoft.Phone.Reactive.Observable.FromEvent<DownloadStringCompletedEventArgs>(m, "DownloadStringCompleted").Subscribe(l =>
{
try
{
//List<NewsKeys> deserialized = JsonConvert.DeserializeObject<List<NewsKeys>>(r.EventArgs.Result);
ObservableCollection<NewsKeys> deserialized = JsonConvert.DeserializeObject<List<NewsKeys>>(l.EventArgs.Result);
foreach (NewsKeys item in deserialized)
{
items.Add(new NewsKeys { nId = item.nId, title = item.title, shortDesc = item.shortDesc, fullDesc = item.fullDesc, tags = item.tags, smallPic = item.smallPic, bigPic = item.bigPic, video = item.video, audio = item.audio, youtube = item.youtube, doc = item.doc, date_create = item.date_create, date_modify = item.date_modify, date_publish = item.date_publish, catId = item.catId, viewOrder = item.viewOrder, viewCount = item.viewCount, viewStatus = item.viewStatus, viewHome = item.viewHome, uId = item.uId, uFname = item.uFname });
}
}
catch (Exception)
{
MessageBox.Show("Sorry, Some unexpected error.");
}
});
m.DownloadStringAsync(new Uri(Resource.NEWS_API+cat_id+"&page="+page));
}
The error i get is
Error 1 Cannot convert lambda expression to type 'System.IObserver>' because it is not a delegate type C:\Users\Adodis\Documents\Visual Studio 2010\Projects\TV\NewsListPage.xaml.cs 51 133
I have tried all the fixes but unable to fix this problem. Am using the same block in different method in a different class it is working fine but, this method in this class killing me. Please help me if you have idea on this.
Thanks in advance.
Try this (I've separated the Select and Subscribe operations):
var m = new SharpGIS.GZipWebClient();
Observable.FromEvent<DownloadStringCompletedEventArgs>(m, "DownloadStringCompleted")
.Select(l => l.EventArgs.Result)
.Subscribe(res =>
{
try
{
var deserialized = JsonConvert.DeserializeObject<List<NewsKeys>>(res);
foreach (NewsKeys item in deserialized)
{
items.Add(
new NewsKeys
{
nId = item.nId,
title = item.title,
shortDesc = item.shortDesc,
fullDesc = item.fullDesc,
tags = item.tags,
smallPic = item.smallPic,
bigPic = item.bigPic,
video = item.video,
audio = item.audio,
youtube = item.youtube,
doc = item.doc,
date_create = item.date_create,
date_modify = item.date_modify,
date_publish = item.date_publish,
catId = item.catId,
viewOrder = item.viewOrder,
viewCount = item.viewCount,
viewStatus = item.viewStatus,
viewHome = item.viewHome,
uId = item.uId,
uFname = item.uFname
});
}
}
catch (Exception)
{
MessageBox.Show("Sorry, Some unexpected error.");
}
});
m.DownloadStringAsync(new Uri("Resource.NEWS_API" + cat_id + "&page=" + page));