播放json:更新数组内的对象属性时出错 [英] Play-json: Error on updating an object attribute inside an array

查看:115
本文介绍了播放json:更新数组内的对象属性时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用play-json库来操纵我的json文件.我已遵循此官方文件,并且一切正常,但他们没有告诉当我们想更新数组中对象的值时,我尝试使用我自己的示例.

I am playing with play-json library to manipulate my json files. I have followed this official document and everything is working fine but they haven't told the case when we want to update a value of an object inside an array so I am trying this my own example.

[
  {
    "key1": [
      {
        "key12": [
          {
            "key121": "text1",
            "key122": 121212,
            "key123": "text2",
            "key124": []
          }
        ],
        "key13": 0
      }
    ],
    "key2": "value2"
  }
]

在上面的示例中,我试图更新"key123"的值,但是一次又一次地收到此错误,只是无法找出我在哪里做错了:(

In above example I am trying to update value of "key123" but getting this error again and again, just not able to find out where am I doing wrong:(

线程主"中的异常java.util.NoSuchElementException:JsError.get 在play.api.libs.json.JsError.get(JsResult.scala:14) 在play.api.libs.json.JsError.get(JsResult.scala:13) 在Refactor.authPage(Refactor.scala:73) 在Builder $ .main(Builder.scala:36) 在Builder.main(Builder.scala) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:483) 在com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

Exception in thread "main" java.util.NoSuchElementException: JsError.get at play.api.libs.json.JsError.get(JsResult.scala:14) at play.api.libs.json.JsError.get(JsResult.scala:13) at Refactor.authPage(Refactor.scala:73) at Builder$.main(Builder.scala:36) at Builder.main(Builder.scala) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

这是我的代码.任何帮助将不胜感激.

Here is my code. Any kind of help would be appreciated.

val transform = (((__ (0) \ 'key1)(0) \ 'key12)(0) \ 'key123).json.update(__.read[JsString].map(x => JsString("updated")))

val jsValue = Json.parse("example.json").transform(transform).get

推荐答案

我已经在互联网上进行了一些研究,发现这个问题已经在播放邮件列表中进行了讨论,即使存在

I have done some research on internet and found that this issue has been already discussed on play mailing list, even there was a ticket to fix this problem. play-json has no native support to update value inside an array (you can do but it would then require lot of handful work, you need to update complete JsArray).

但是play-json目前不支持基于数组索引的转换,但有一个帮助器库 play-json-zipper ,它支持所有此类功能以及更多功能.假设人们可以根据需要使用play-json-zipper,则Play开发人员已关闭上述票证.

However play-json has no current support for transformation based on array indices, there is a helper-library play-json-zipper, which support all such feature and a little more. Play developers have closed the above mentioned ticket, assuming people can use play-json-zipper if required.

P.S:添加我自己的答案,以便如果其他人通过google搜索降落在这里,他们可以轻松找到解决方案.

P.S: Adding my own answer so that if anyone else lands here by google search they can easily find the solution.

这篇关于播放json:更新数组内的对象属性时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆