首页 \ 问答 \ 展平Scala中嵌套的Json对象列表(Flattening a List of nested Json objects in Scala)

展平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
更新时间:2023-07-29 08:07

最满意答案

首先让我们解决分页问题。 现在分页不可撤销,但它确实要求您的服务器接受页面,以便它知道返回哪组结果。 例如,如果您的组大小为20,则有100个结果,在您的网址请求中,您必须指定是否需要第0 - 4页。

我个人建议使用一个开源库来为你处理分页。 看看MMRecord ,它确实为你提供了一些分页支持。 如果您有野心想要并且想要自己实现它,那么您需要构建一些网络服务,最好建立在AFNetworking之类的内容上 ,以跟踪最后请求的页面。

在swift中,这看起来像这样:

NetworkManager.GET('http://someURI', parameters: [:], page: 0)

不知何故,你必须保持下一个页面的状态。 如此处所建议的,更好的设计是使用协议和某种数据源包装器,它从客户端抽象出机制。

现在,您还询问了如何检测表视图何时滚动到末尾,以便您可以触发网络请求。 一种方法是简单观察scrollViewDidScrollUITableView继承自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 from UIScrollView) and check to see if the contentOffset 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 {}

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)