展平Scala中嵌套的Json对象列表(Flattening a List of nested Json objects in Scala)
我有一个这种形式的Json对象列表:
[{'a': JsonObj()}, {'b':JsonObj()}, .... ]
如何将列表展平为此表单:
{'a': JsonObj(), 'b': JsonObj(), ...}
请注意,每个字段都有一个嵌套的JsonObj作为其值。 我不想展平那些json对象。
我想把它附加到一个空的JsonObj但是不起作用。
val rStreetsJsonObj: JsObject = Json.obj() for (routeS <- jsonList) { rStreetsJsonObj.+(routeS) // Gives error: expected arguments should be of the form (String, jsValue) not JsObject }
有任何想法吗?
I have a list of Json Objects in this form:
[{'a': JsonObj()}, {'b':JsonObj()}, .... ]
How do I flatten the list into this form:
{'a': JsonObj(), 'b': JsonObj(), ...}
Note each field has a nested JsonObj as its value. I do not want to flatten those json objects.
I thought of appending it into a empty JsonObj but doesn't work.
val rStreetsJsonObj: JsObject = Json.obj() for (routeS <- jsonList) { rStreetsJsonObj.+(routeS) // Gives error: expected arguments should be of the form (String, jsValue) not JsObject }
Any ideas?
原文:https://stackoverflow.com/questions/43902619
最满意答案
首先让我们解决分页问题。 现在分页不可撤销,但它确实要求您的服务器接受页面,以便它知道返回哪组结果。 例如,如果您的组大小为20,则有100个结果,在您的网址请求中,您必须指定是否需要第
0 - 4
页。我个人建议使用一个开源库来为你处理分页。 看看MMRecord ,它确实为你提供了一些分页支持。 如果您有野心想要并且想要自己实现它,那么您需要构建一些网络服务,最好建立在AFNetworking之类的内容上 ,以跟踪最后请求的页面。
在swift中,这看起来像这样:
NetworkManager.GET('http://someURI', parameters: [:], page: 0)
不知何故,你必须保持下一个页面的状态。 如此处所建议的,更好的设计是使用协议和某种数据源包装器,它从客户端抽象出机制。
现在,您还询问了如何检测表视图何时滚动到末尾,以便您可以触发网络请求。 一种方法是简单观察
scrollViewDidScroll
(UITableView
继承自UIScrollView
)并检查contentOffset
是否>= self.tableView.contentSize - self.tableView.frame.size.height
(这将是当时的内容偏移量)表一直滚动)。 但是,您希望应用程序的用户能够继续滚动而不必滚动到底部,等待它加载,然后继续滚动,这样当我们向页面向下滚动3/4时我会发出请求如果您知道请求将需要很长时间,甚至中途。 这样,当它们到达底部时,它已经加载了数据,用户永远不会知道差异。编辑:
除了我的回答,我会为你添加一些建议。 1)看看这个: http : //artsy.github.io/blog/2015/09/24/mvvm-in-swift/
它讨论了MVVM设计模式,它更适合保持状态而不是视图控制器中的状态变量。
2)你的函数,
fetchAllObjectsFromParse
是一个坏名字,原因有两个。 首先,该名称暗示该函数一次性获取所有对象,如果您希望对其进行分页,则不是这种情况。也许是这样的:
func fetchObjectsForModel(model: String, atPage page: Int, withBlock block: ([PFObejct], NSError?) -> Void { }
然后创建另一个将生成您的查询的函数:
func generateQueryWithSkips(skip: Int, limit: Int) -> PFQuery {}
First let's tackle pagination. Now pagination isn't undoable but it does require your server to accept pages so that it knows which set of results to return. For example if you have 100 results in group sizes of twenty, in your url request you'd have to specify if you wanted page
0 - 4
.I would personally recommend using an open-source library to take care of pagination for you. Take a look at MMRecord, it does have some pagination support for you. If you are feeling ambitious and you want to implement it on your own, you'll need to build some networking service, best built on something like AFNetworking that keeps track of the last requested page.
In swift, that would look something like this:
NetworkManager.GET('http://someURI', parameters: [:], page: 0)
Somehow you have to keep the state of which page is next around. A better design, as suggested here, is to use protocols and some sort of datasource wrapper that abstracts the mechanism from the client.
Now you also asked how to detect when a table view scrolls to the end so you can fire off a network request. One way to do it is simple observe
scrollViewDidScroll
(UITableView
inherits fromUIScrollView
) and check to see if thecontentOffset
is>= self.tableView.contentSize - self.tableView.frame.size.height
(which will be the content offset of when the table is all the way scrolled). However you want the user of your application to be able to keep scrolling without having to scroll to the bottom, waiting for it to load, then continue scrolling so what I would is fire the request when they've scrolled 3/4 down the page or even halfway if you know the request will take a long time. That way by the time they get to the bottom, it'll already have loaded the data and the user will never know the difference.EDIT:
I would add a few suggestions for you apart from my answer. 1) Take a look at this: http://artsy.github.io/blog/2015/09/24/mvvm-in-swift/
It talks about the MVVM design pattern which is much better for holding state instead of having state variables in your view controller.
2) your function,
fetchAllObjectsFromParse
is a bad name for a couple of reasons. First, the name implies that the function fetches all the objects at once, which isn't the case if you want it paginated.Maybe something like this:
func fetchObjectsForModel(model: String, atPage page: Int, withBlock block: ([PFObejct], NSError?) -> Void { }
And then make another function which will generate your query:
func generateQueryWithSkips(skip: Int, limit: Int) -> PFQuery {}
相关问答
更多-
假设没有重复, id selectedObject = [searchArray objectAtIndex:indexPath.row]; index = [sourceArray indexOfObject:selectedObject]; 想法很简单。 获取搜索数组的选定对象,因为它将直接映射到索引路径的行。 然后在源或主数组中搜索对象的索引。 这应该给你你想要的索引。 Assuming no duplicates, id selectedObject = [searchArray objectAt ...
-
你需要检查你的阵列。 从我在这里看到的, numOfDays少了元素。 此外,当你在那里检查creditArray以及:) 此外,不需要NSIndexPath为NSIndexPath 。 You need to check your arrays. From what I can see here, numOfDays have less elements then nameArray. Also, while you are there check creditArray as well :) Also, ...
-
[selectedRowsInSectionDictionary objectForKey:@(indexPath.section)]是一个NSMutableArray引用,而不是一个indexPath ,所以比较永远不会是真的。 我建议你将NSMutableIndexSet存储在你的字典中,而不是一个数组。 你的代码会是这样的: NSMutableIndexSet *selectedSet = selectedRowsInSectionDictionary[@(indexPath.section)]; i ...
-
尝试在设置单元格时使用self.tableView.rowHeight ,如果有任何问题回拨,我尝试使用下面的示例代码进行应用。 case "ExtraFieldCell": let currentField = self.extraFields[indexPath.row - self.firstExtraFieldIndex] switch currentField.type { case "select": se ...
-
你的代码有点太棘手了。 有时通常的循环更容易。 [dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL * stop) { for( NSInteger index = 0; index < [obj count]; index++ ) { if ([obj[index] isEqualToString:uid]) { // thats the index // Let's have a ...
-
问题是tableView(_:canEditRowAt:)方法来自UITableViewDataSource协议,而不是UITableViewDelegate协议。 将其移动到另一个分机。 The problem is that the tableView(_:canEditRowAt:) method is from the UITableViewDataSource protocol, not the UITableViewDelegate protocol. Move it to the other ...
-
您必须实例化高度值 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { var height:CGFloat = 100 switch indexPath.row { case 5: height = 190 case 6: height = 140 default: break ...
-
首先让我们解决分页问题。 现在分页不可撤销,但它确实要求您的服务器接受页面,以便它知道返回哪组结果。 例如,如果您的组大小为20,则有100个结果,在您的网址请求中,您必须指定是否需要第0 - 4页。 我个人建议使用一个开源库来为你处理分页。 看看MMRecord ,它确实为你提供了一些分页支持。 如果您有野心想要并且想要自己实现它,那么您需要构建一些网络服务,最好建立在AFNetworking之类的内容上 ,以跟踪最后请求的页面。 在swift中,这看起来像这样: NetworkManager.GET(' ...
-
TableView分为几个部分。 每个部分包含一些行。 IndexPath包含有关函数询问哪个部分的行的信息。 根据此数字,您将配置单元格以显示给定行的数据。 应用程序在需要显示行时执行此功能。 例如,您向下滚动桌面视图,下一个单元格将出现在屏幕上。 例如,要使用正确的文本配置此单元格,它会询问您应该在该位置(部分和行)的行上显示的内容。 此外,为了提高性能,您应该使用tableView.dequeueReusableCellWithIdentifier函数而不是Cell的init来创建单元格。 Table ...
-
你可以使用这个获得indexpath数组。 NSArray * indexPathArray = [yourTableView indexPathsForSelectedRows]; you can get indexpath array using this. NSArray *indexPathArray = [yourTableView indexPathsForSelectedRows];