I have a nested Map that's look like this :
{users: {1: {id: 1, name: Bill, date: 2020-01-01 00:00:00.000Z}}}
I want access to the name
array["users"]["1"]["name"]
This is working fine. But I want to do it "dynamically", like something :
final path = "users,1,name";
array[path];
Is there a way to do that in Dart ?
Maybe this way:
final path = 'users,1,name';
final parts = path.split(',');
print(array[parts[0]][parts[1]][parts[2]]);
Related
I have a Map object
Map decodedresp = json.decode(response);
from here i get a list :
print(decodedresp['objectUrls']);
[{signedUrl: abc.com, path: a/b/c.log}]
now I want to get abc.com in a string, and I am not able to get it
I am learning dart and I am new to it
So, from what I can see it looks like you're dealing with parsed json.
In decoding json like you did json.decode(response) you're getting a:
_InternalLinkedHashMap<String, dynamic>
From that you are accessing objectUrls which returns a List<Map>. In order to deal with that you need to look for the key which in this case is signedUrl or path. Because your Map is wrapped in a List you need to get the element of the List.
This should work for you
Map<String, dynamic> decodresp = json.decode(response);
List<dynamic> objectUrls = decoderesp['objectUrls'];
// Zero here to get the first element, followed by the key.
var signedUrl = objectUrls[0]["signedUrl"];
var path = objectUrls[0]["path"];
You can see it working here
I could be wrong but because there isn't much info this is what I'm going with.
Let me know if you have any problems.
Felix.
Why this works:
def m = [[1,11], [2,22], [3,33]]
println(m.collectEntries())
output: [1:11, 2:22, 3:33]
But this doesn't work:
def m = [[Name:sub, Value:23234], [Name:zoneinfo, Value:Europe/London]]
println(m.collectEntries())
output:
groovy.lang.MissingPropertyException: No such property: sub for class
I want to process that map so that I get a list of key value pairs like this:
["Name:sub" :"Value:23234", "Name:zoneinfo": "Value:Europe/London"]
where Name:sub is the key and Value:23234 is the value.
Reference https://stackoverflow.com/a/34899177/9992516
In the second example sub and zoneinfo are being read as variable names, not strings, and you need to quote them.
def m = [[Name:'sub', Value:23234], [Name:'zoneinfo', Value:'Europe/London']]
println m.collectEntries{ ["Name:${it.Name}", "Value:${it.Value}"] }
It cannot find sub field in your class, probably you want to have a string "sub"?
Basically, map entry can be declared in two ways:
Name: 'sub'
and
'Name': 'sub'
For the key it is assumed that is is a String, even if it is not wrapped by quotes.
But for the value it is mandatory to wrap in quotes. Otherwise, it is treated as a variable (or field)
Given your desired results:
["Name:sub" :"Value:23234", "Name:zoneinfo": "Value:Europe/London"]
What you actually need to do is quote the entire item in each pair:
def m = [["Name:sub", "Value:23234"], ["Name:zoneinfo", "Value:Europe/London"]]
I have a model defined in Laravel 5.1. For some reason, I can't seem to get the id from it:
Psy Shell v0.5.2 (PHP 5.6.13-0+deb8u1 — cli) by Justin Hileman
>>> $survey = \App\Survey::where('id',54)->get();
=> Illuminate\Database\Eloquent\Collection {#682
all: [
App\Survey {#693
id: 54,
created_at: "2015-10-12 03:18:32",
updated_at: "2015-10-12 03:18:32",
location_id: 0,
},
],
}
>>> echo $survey->id;
PHP error: Undefined property: Illuminate\Database\Eloquent\Collection::$id on line 1
>>>
What am I doing wrong?
You are using the 'get' method for retrieving your recrods. But in fact the 'get' method returns a collection of models.
So in order to get your id you need to use for example a foreach statement to loop over the collection.
But you could also use another option that will give you just a single model instance.
For example:
$survey = \App\Survey::where('id',54)->first();
$survey = \App\Survey::find(54);
Both will return a single model instance.
So after this you can just use:
echo $survey->id;
To access your id.
See this link for more information about this topic: Retrieving Single Models / Aggregates
You are trying to access the id on collection and it will throw an error. Try modifying the query to
$survey = \App\Survey::where('id',54)->first();
by replacing get() by first() and it will work fine.
Or you can use find() to get the record by primary key $survey = \App\Survey::find(54);
Is like or rlike supported for searching a string in a collection's property value?
Does the collection need to define text type index for this to work? Unfortunately I can not create a text index for the property. There are 100 million documents and text index killed the performance (MongoDB is on single node). If this is not do-able without text index, its fine with me. I will look for alternatives.
Given below collection:
Message {
'payload' : 'XML or JSON string'
//few other properties
}
In grails, I created a Criteria to return me a list of documents which contain a specific string in the payload
Message.list {
projections {
like('payload' : searchString)
}
}
I tried using rlike('payload' : ".*${searchString}.*") as well. It did not result in any doc to me.
Note: I was able to get the document when I fired the native query on Mongo shell.
db.Message.find({payload : { $regex : ".*My search string.*" }}).pretty()
I got it working in a round about way. I believe there is a much better grails solution. Criteria approach did not work. So used the low level API converted the DBObjects to Domain objects.
def query = ['payload' : [ '$regex' : /${searchString}/ ] ]
def dbObjects = Message.collection.find(query).skip(offset).limit(defaultPageSize).toArray()
dbObjects?.collect { new Message(new JsonSlurper().parseText(it.toString()))}
How can I get all nodes by specific Document Type?
For example, I want to get in code behind all of nodes with Document Type: s3Article. How can I do this?
New informations:
IEnumerable<Node> nodes = uQuery.GetNodesByType("s3Article").Where(x => x.NiceUrl.Contains("en"));
lvArticles.DataSource = nodes;
lvArticles.DataBind();
This is my code. I had to use Where(x => x.NiceUrl.Contains("en")), because I have 2 language version- without Where I receive nodes from all catalogues with doctype s3Article, but I want to get only from one language version.
Problem is here:
<a href='<%# umbraco.library.NiceUrl(Tools.NumericTools.tryParseInt( Eval("id"))) %>'><%# Eval("title")%></a>
<%# Tools.TextTools.makeIMGHTML("../.."+ Eval("img").ToString(),"180") %>
<%# umbraco.library.StripHtml(Limit(Eval("Article"), 1000))%>
<%# Eval("author")%>
System.Web.HttpException: DataBinding:
'umbraco.presentation.nodeFactory.Node' does not contain a property named 'title'.
The same problem happens with the title, img, article, author. Only ID works nice. How to resolve it?
You can use the uQuery GetNodesByType(string or int) method:
IEnumerable<Node> nodes = uQuery.GetNodesByType("s3Article");
Alternatively, you can use an extension method to get all descendant nodes and then query them by type as in the following answer:
Umbraco 4.6+ - How to get all nodes by doctype in C#?
You could use this to databind to a control within a usercontrol like so:
lvArticles.DataSource = nodes.Select(n => new {
ID: n.Id,
Title: n.GetProperty("title").Value,
Author: n.GetProperty("author").Value,
Article: n.GetProperty("article").Value,
Image: n.GetProperty("img").Value,
});
lvArticles.DataBind();
Only you would need to strip the html, convert the image id to a url, etc. within the select statement as well...
As Shannon Deminick mentions, uQuery is somewhat obsolete. ExamineManager will be the fastest execution time. https://our.umbraco.org/forum/developers/api-questions/45777-uQuery-vs-Examine-vs-IPublishedContent-for-Querying
I also found it to be the easiest and most readable approach to use ExamineManager's search builder. Very flexible, and has the added benefit of being very readable due to the Fluent Builder pattern the U Team used.
This will search ALL nodes, so if you need within a specific branch, you can use .ParentId(1234) etc.
var query = ExamineManager.Instance.CreateSearchCriteria()
.NodeTypeAlias("yourDocumentType")
.Compile();
IEnumerable<IPublishedContent> myNodes = Umbraco.TypedSearch(query);
I prefer typed nodes, but you can also just use "Search()" instead of "TypedSearch()" if you prefer dynamic nodes.
Another example including a specific property value "myPropValue" == "ABC",
var query = ExamineManager.Instance.CreateSearchCriteria()
.NodeTypeAlias("yourDocumentType")
.Or() //Other predicate .And, .Not etc.
.Field("myPropValue", "ABC")
.Compile();
Ref - https://our.umbraco.org/documentation/reference/querying/umbracohelper/