I have json that looks like:
myjson = {"queries":{"F.SP": 27}}
so with
queryResults = JObject.Parse(jsonString)
I can do
firstToken = queryResults.SelectToken("queries")
and get back the LinqJToken
{"F.SP": 27}
but I'm then stuck, because when I try
subToken = firstToken.SelectToken("F.SP")
I get Nothing. I'm guessing this is because JSON.net is looking for a token "F" with subtoken "SP".
I've also tried each of the following, to no avail
myToken = queryResults.SelectToken("queries.F.SP")
myToken = queryResults.SelectToken("queries[0].F.SP")
(queryResults.SelectToken("queries[0]") returns nothing, fwiw)
Any ideas?
EDIT: I have verified that the embedded "." is the problem; if I change the original json to
{"queries":{"FSP": 27}}
I can do
queryResults.SelectToken("queries").SelectToken("FSP")
no problem
If you have such names in JSON fields:
{"queries":{"F.SP": 27}}
You may use SelectToken escaping:
queryResults.SelectToken("queries").SelectToken("['F.SP']")
or
queryResults.SelectToken("queries.['F.SP']")
Here are more examples with escaping: http://www.newtonsoft.com/json/help/html/QueryJsonSelectTokenEscaped.htm
This won't return the token itself, but will return the value (which is probably what you're looking for anyway)...
queryResults.SelectToken("queries").Value<int>("F.SP");
Instead of trying to use SelectToken, how about an index search?
subToken = queryResults["F.SP"];
JObject obj = JObject.Parse(jsonstring);
var fsp = obj["queries"].First().First();
Not the most elegant but it gets the value.
Related
I'm trying to use a parameter inside of a JSON string, and would like to use an inner parameter to replace an GUID. I've changed the default parameter start and end characters since curly braces are used in JSON.
I've tried to do something like this, where the json param contains my json which is similar to this below.
{"DashboardGUID":"<Dash_GUID>"}
request_json = lr_eval_string("<json>");
lr_save_string(request_json, "request_json_param");
I'm expecting the lr_eval_string to replace the with the GUID that's in this parameter, what's the best why of replacing this ID in my JSON String?
Not sure what you are asking but I will put this here in case someone comes here in the future:
main.c
Action()
{
lr_eval_json("Buffer/File=my_json.json", "JsonObject=MJO",LAST);
lr_json_stringify("JsonObject=MJO","Format=compact", "OutputParam=newJsonBody",LAST);
lr_save_string(lr_eval_string(lr_eval_string("{newJsonBody}")),"tmp");
web_reg_find("Text={mydate}",LAST);
web_rest("POST",
"URL=http://myServer.microfocus.com/url",
"Method=POST",
"EncType=raw",
"Body={tmp}",
HEADERS,
"Name=Content-Type", "Value=application/json", ENDHEADER,
LAST);
return 0;
}
my_json.json
{
"LastActionId": 0,
"Updated": "{mydate}"
}
Okay so instead of doing what I'm thinking above I ended up creating an array of char's with this {"DashboardGUID":"<Dash_GUID>", someotherdata:"123"} in 10 different positions within the array. I then randomly selected an element from this array and when doing the lr_eval_string the parameter was replaced.
Hopefully this makes sense those looking to do something similar.
I'm having difficulty with the following.
In VB.Net, I have the following line:
Dim intWidgetID As Integer = CType(Replace(strWidget, "portlet_", ""), Integer)
where strWidget = portlet_n
where n can be any whole number, i.e.
portlet_5
I am trying to convert this code to C#, but I keep getting errors, I currently have this:
intTabID = Convert.ToInt32(Strings.Replace(strTabID, "tab_group_", ""));
which I got using an online converter
But it doesn't like Strings
So my question is, how to I replace part of a string, so intTabID becomes 5 based on this example?
I've done a search for this, and found this link:
C# Replace part of a string
Can this not be done without regular expressions in c#, so basically, I'm trying to produce code as similar as possible to the original vb.net example code.
It should be like this strTabID.Replace("tab_group_", string.Empty);
int intTabID = 0;
string value = strTabID.Replace("tab_group_", string.Empty);
int.TryParse(value, out intTabID);
if (intTabID > 0)
{
}
And in your code i think you need to replace "tab_group_" with "portlet_"
Instead of Strings.Replace(strTabID, "tab_group_", ""), use strTabID.Replace("tab_group_", "").
This should work
int intWidgetID = int.Parse(strTabID.Replace("tab_group_",""));//Can also use TryParse
Their is no Strings class in Vb.Net so please use the string class instead http://msdn.microsoft.com/en-us/library/aa903372(v=vs.71).aspx
you can achieve it by this way
string strWidget = "portlet_n";
int intWidgetID = Convert.ToInt32(strWidget.Split('_')[1]);
I need to know how I can parse a variable path in Flex 3 & e4X. For example, I have two XML strings where the name of one element is the only difference.
<NameOfRoot>
<NameOfChild1>
<data>1</data>
</NameOfChild1>
</NameOfRoot>
<NameOfRoot>
<NameOfChild2>
<data>2</data>
</NameOfChild2>
</NameOfRoot>
Currently I am accessing variables like this:
var data1:String = NameOfRoot.*::NameOfChild1.*::data;
var data2:String = NameOfRoot.*::NameOfChild2.*::data;
I would rather make this task more abstract so that if "NameOfChild3" is introduced I do not need to update the code. For example:
var data:String = NameOfRoot.*::{variable}.*::data;
Does anyone have insights into how this can be done?
Use the child property (LiveDocs example here):
var tagName:String = "NameOfChild1";
var data:String = NameOfRoot.child(tagName).data;
That's with no namespacing--not sure whether it's necessary in your case, but I assume you'd add some *::'s?
this also works:
var data:String = NameOfRoot..data;
but if you have more than 1 data node you'll have to sort some stuff out.
It looks like the ".*." operation will work. I wonder if this is the easiest way to handle this problem.
var data:String = NameOfRoot.*.*::data;
I am trying to access an XMLList item and convert it to am XML object.
I am using this expression:
masonicXML.item.(#style_number == styleNum)
For example if there is a match everything works fine but if there is not a match then I get an error when I try cast it as XML saying that it has to be well formed. So I need to make sure that the expression gets a match before I cast it as XML. I tried setting it to an XMLList variable and checking if it as a text() propertie like this:
var defaultItem:XMLList = DataModel.instance.masonicXML.item.(#style_number == styleNum);
if(defaultItem.text())
{
DataModel.instance.selectedItem = XML(defaultItem);
}
But it still give me an error if theres no match. It works fine if there is a match.
THANKS!
In my experience, the simplest way to check for results is to grab the 0th element of the list and see if it's null.
Here is your code sample with a few tweaks. Notice that I've changed the type of defaultItem from XMLList to XML, and I'm assigning it to the 0th element of the list.
var defaultItem:XML =
DataModel.instance.masonicXML.item.(#style_number == styleNum)[0];
if( defaultItem != null )
{
DataModel.instance.selectedItem = defaultItem;
}
OK I got it to work with this:
if(String(defaultItem.#style_number).length)
Matt's null check is a good solution. (Unless there is the possibility of having null items within an XMLList.. probably not, but I haven't verified this.)
You can also check for the length of the XMLList without casting it to a String:
if (defaultItem.#style_number.length() > 0)
The difference to String and Array is that with an XMLList, length() is a method instead of a property.
So I have something like this:
var xmlStatement:String = "xmlObject.node[3].#thisValue";
What mystery function do I have to use so that I can execute xmlStatement and get thisValue from that xmlObject? Like....
var attribute:String = mysteryFunction(xmlStatement);
P.S. I know eval() works for actionscript2, I need the as3 solution. :)
Unfortunately this is not possible in ActionScript 3. This however might be a solution: http://blog.betabong.com/2008/09/23/e4x-string-parser/
For your example it would be:
var attribute : String = String( E4X.evaluate( XMLList(xmlobject) , 'node[3].#thisValue' ) );