Accessing element of a JArray -without Foreach loop - json

I'm developing an application where I receive a JSON array from an API. It has only one object. How do I access it in a single line syntax without using forEach loop?
object is inside array
[{"fiscalPeriod":8,"fiscalYear":2018,"calendarStart":"2018-02-11","calendarEnd":"2018-03-10"}]

Since you only have one item in the array you can get it out like this:
var array = [{"fiscalPeriod":8,"fiscalYear":2018,"calendarStart":"2018-02-11","calendarEnd":"2018-03-10"}];
var obj = array[0];
That square bracket syntax with the 0 in it says: give me the first item in the array.

Here some examples
var arr = [{"fiscalPeriod":8,"fiscalYear":2018,"calendarStart":"2018-02-11","calendarEnd":"2018-03-10"}];
console.log(arr[0].fiscalPeriod); // logs '8'
console.log(arr[0].fiscalYear); // logs '2018'
console.log(Object.keys(arr[0])); //["fiscalPeriod", "fiscalYear", "calendarStart", "calendarEnd"]
console.log(Object.keys(arr[0])[2]); //logs "calendarStart"

Related

Json output string into an array or list

Hi i am trying to set json output to an array or list, but receiving an error
"cannot convert from system.collections.generic.list to String[]"
String received in paramValue-
{"0":["1234","2222","4321","211000","90024","12","2121","322223","2332","3232"],"1":["0856","6040222","175002","23572","","","","","",""]}
List<string> strlist;
strlist = new List<string>();
var jr = JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(paramValue);
foreach (var item in jr)
{
//Need some thing like this
// strList.Add(item)
}
In order to take "item" values in the List, I also tried item.Value.CopyTo(strlist); but got the error "cannot convert from system.collections.generic.list to String[]".
Actually i need the "item" values to be stored in List, so that i can pass into another functions.
Thanks
When you iterate over the dictionary you have the List<string> in the value, so you can not add that value array using add to the List<string> strlist; add expect it to be a single value not multiple, as you want to add multiple values you can use AddRange.
like this
foreach (var item in jr)
{
//Need some thing like this
strlist.AddRange(item.Value);
}
if you want to use short-one then use
strlist.AddRange(jr.SelectMany(a => a.Value));

Node-red payload convertion for mysql node

How to convert a msg.payload like 12345,67890 into "12345","67890" ?
Basically I receive a payload with many values and need to pass them to a mysql node.
Thanks in advance.
Sep
Depending on the exact type of input there are a couple options
First the core CSV node will split a payload into either an array or an object (if the first line of multiple lines contains the column names).
Second if you just have a single input then you can break it up with a function node using the normal NodeJS/Javascript functions for working with Strings. e.g.
var chunks = msg.payload.split(',');
msg.payload = {};
msg.paylaod.first = chunks[0];
msg.payload.second = chunk[1];
return msg;

Angular2 IndexOf Finding and Deleting Array Value

Hello I'm attempting delete a certain index in my array while using Angular2 and Typescript. I would like to retrieve the index from the value.
My array is declared normally...
RightList = [
'Fourth property',
'Fifth property',
'Sixth property',
]
I start out with a basic premise to set up my remove function.
removeSel(llist: ListComponent, rlist:ListComponent){
this.selectedllist = llist;
console.log(JSON.stringify(this.selectedllist)); // what is to be searched turned into a string so that it may actually be used
My console.log of my JSON.Stringify tells me that the value that I will attempt to remove is "Sixth property". However when I attempt to look for this value in my array using the following code. It returns -1 which means my value is not found in the array.
var rightDel = this.RightList.indexOf((JSON.stringify(this.selectedllist))); // -1 is not found 1 = found
console.log(rightDel);
On my output to the console it does return the item that is to be searched for but does not find the item in the array
CONSOLE OUTPUT:
"Sixth property" // Item to be searched for
-1 // not found
Is there something wrong in my implementation of my function that searches the array?
Of course indexOf will not find you item in array because
JSON.stringify(this.selectedllist) !== this.selectedllist
This is because JSON stringified string encodes quotes around literal, that original string doesn't have. It is easy to test:
var a = 'test';
console.log( JSON.stringify(a), a, JSON.stringify(a) === a )
Remove JSON.stringify and it should work. In general, to cast something to String type you should use its .toString() method, or simply wrap this something into String(something).

How to expect a new arraylist object in powermock?

I have a code which has below two new List objects of different type:
List<TypeOne> typeOneList = new ArrayList<TypeOne>();
List<TypeTwo> typeTwoList = new ArrayList<TypeTwo>();
How can I use PowerMock.expectNew() to return two different ArrayList objects? Like..
PowerMock.expectNew(ArrayList.class).andReturn(typeOneList);
PowerMock.expectNew(ArrayList.class).andReturn(typeTwoList);
How we can differentiate in the above statement as for which statement the objects corresponds to?
thanks!
Well. You have to define in the below way in the order you want..
PowerMock.expectNew(ArrayList.class).andReturn(typeOneList); //first expect list object of TypeOne
PowerMock.expectNew(ArrayList.class).andReturn(typeTwoList); //then expect list object of TypeTwo
and powermock will return the objects in the order of expectations when the below code executes:
List<TypeOne> typeOneList = new ArrayList<TypeOne>();
List<TypeTwo> typeTwoList = new ArrayList<TypeTwo>();

AS3 Custom string formula parser

My goal is to create some kind of Parser to parse string formulas, similar to Excel formulas.
Formula string example (barcode example) -
"CONCAT('98', ZEROFILL([productNumber],5,'0'), ZEROFILL(EQUATION([weightKG]*1000),5,'0'))"
where
'98' - String
[productNumber] and [weightKG] - are variables that can be changed
CONCAT, ZEROFILL and EQUATION are methods which exist in class
For this formula with variables [productNumber] = '1' and [weightKG] = 0.1 result must be
'980000100100'
The question is how to split/parse whole string to parts and detect methods, variables and string values?
Another idea occurred, while i was typing - is to store whole formula in XML format.
Thank You.
You can use String.split() to get an array of substrings.
However, using your example, calling split(",") would give you the following array:
[0]=CONCAT('98'
[1]= ZEROFILL([productNumber]
[2]=5
[3]='0')
[4]= ZEROFILL(EQUATION([weightKG]*1000)
[5]=5
[6]='0'))
That doesn't seem like it will be very helpful for your project. Instead, you might think about creating a parse() function with some logic to find useful substrings:
function parse(input:String):Array {
var firstParen:int = input.indexOf("(");
var lastParen:int = input.lastIndexOf(")");
var formulaName:String = input.substring(0, firstParen);
var arguments:String = input.substring(firstParen, lastParen);
var argumentList:Array = parseArgs(arguments);
var result:Array = new Array();
result.push(formulaName);
//Recursively call parse() on the argumentList
foreach (var elem:* in argumentList) {
result.push(elem); //Could be string or array.
}
}
function parseArgs(input:String):Array {
// Look for commas that aren't enclosed inside parenthesis and
// construct an array of substrings based on that.
//A regex may be helpful here, but the implementation is left
//as an exercise for the reader.
}