- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试 Scala,尤其是 json4s lib 以便操作一些 json。我很难理解 Scala 和 json4s 的语法,我想问问你们。
我有这个 json,我需要更新其中的一些字段,然后完整地发回服务。 json 看起来像这样:
{
"id": "6804",
"signatories": [
{
"id": "12125",
"fields": [
{
"type": "standard",
"name": "fstname",
"value": "John"
},
{
"type": "standard",
"name": "sndname",
"value": "Doe"
},
{
"type": "standard",
"name": "email",
"value": "john.doe@somwhere.com"
},
{
"type": "standard",
"name": "sigco",
"value": "Company"
}
]
}
]
}
我正在使用 json4s将其解析为 JArray,如下所示:
import org.json4s._
import org.json4s.native.JsonMethods._
val data = parse(json)
val fields = (data \ "signatories" \ "fields")
这给了我一个包含所有字段的 JArray:(非常抱歉格式化)
JArray(List(JObject(List((type,JString(standard)), (name,JString(fstname)), (value,JString(John)))), JObject(List((type,JString(standard)), (name,JString(sndname)), (value,JString(Doe)))), JObject(List((type,JString(standard)), (name,JString(email)), (value,JString(john.doe@somwhere.com)))), JObject(List((type,JString(standard)), (name,JString(sigco)), (value,JString(Company))))))
我现在面临的问题是:
如何找到每个字段属性“名称”,并将其更改(转换)为新值?
例如(我知道这在 Scala 中很可能不是这样工作的,但你会明白的)
foreach(field in fields) {
if(field.name == 'fstname') {
field.value = "Bruce"
}
}
最佳答案
你可以试试
val a = JArray(List(JObject(....))) // Same as your JArray<pre><code>
a.transform {
// Each JArray is made of objects. Find fields in the object with key as name and value as fstname
case obj: JObject => obj.findField(_.equals(JField("name", JString("fstname")))) match {
case None => obj //Didn't find the field. Return the same object back to the array
// Found the field. Change the value
case Some(x) => obj.transformField { case JField(k, v) if k == "value" => JField(k, JString("Bruce")) }
}
}
结果 -
res0: org.json4s.JValue = JArray(List(JObject(List((typ,JString(standard)), <strong>(name,JString(fstname)), (value,JString(Bruce))))</strong>, JObject(List((typ,JString(standard)), (name,JString(sndname)), (
ring(Doe)))), JObject(List((typ,JString(standard)), (name,JString(email)), (value,JString(john.doe@somwhere.com)))), JObject(List((typ,JString(standard)), (name,JString(sigco)), (value,JStrin
))))))
关于scala - JArray 中的 TransformField 与 json4s,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27937828/
我有一个 jArray 和来自该 jArray 的字符串“message2”: [{"date":"2012","count":"1","message" : "message1"}, {"date"
我有一个 JArray 类型的对象,其中包含一个 json 对象列表。 JArray 看起来像: JArray jarray = [{"Key1":"v1"}, {"Key2":"v2"}, {"Ke
这是我的代码: JArray first; JArray second; JArray test = first.Union(second); 但是它说它不能在 JToken 和 JArray 之间进
我从服务器接收格式为日期的数组 [{"date":"2016-12-26T00:00:00+08:00"}, {"date":"2016-12-27T00:00:00+08:00"}, {"date"
我有一个字符串数组 var ids = new string[] { "1408576188", "1750854738", "100001058197465" }; 我想将这
我在使用 C#(VS 2017,.Net 4.6)代码时遇到了一些问题。如果有人可以提供帮助,那就太好了。我有一个 JSON 文件: { "makerCommission": 10, "tak
我有以下 JSON [ { "Code": "Global Payroll", "Month1": 1, "Month2": 0,
我有以下 JSON [ { "Code": "Global Payroll", "Month1": 1, "Month2": 0,
我有一个想要添加到的 json 数组,然后将内容写入文件。将内容添加到数组中没有问题,但使用 JsonSerializer.Serialize 方法写入文件给了我一个异常(exception): 参数
通过 http Post,我将 html FormData 发送到我的 Web Api2 Controller 。FormData 包含一个或多个图像,以及客户端属性。 我的前端 Angular 5
我想像下面的数组一样添加,请通过循环帮助我,请帮助我;我的逻辑获取数组的最后一个图像 for (int i = 0; i < jarray.length(); i++) {
我有一个 JSON 数组如下: { "images" : [ { "pid":0, "image_title":"this is title of image1"
当我写这段代码时: string[] title= {"title1","title2","title3","title4"}; int[] iid= {124,122,333,234};
我有一个 JArray,从文件中读取: private void RemoveCatalog(Catalog catalog) { System.IO.StreamReader filerea
我有一个 JSON { "departments": [ { "2": {"city": "Petersburg", "employees": "1200"} },
我正在开发一个 IMDB 类型的程序,我在其中列出了拥有电影、唱片和类似内容的人。所以我已经在 MVVM 中提出了自己的观点,现在我正在尝试序列化我的 json 以匹配它。现在我正在收集我的表格,如下
我的 JSON 看起来与此类似: [{“itemA”:{“name”:”foo”,”other”:"bar”}},{“itemB”:{“name”:”foo2”,”other”:”bar2”}},{“
我想从 Json 字符串解析 JArray。为此,我有这段代码: JObject myjson = JObject.Parse(theJson); JArray nam
我正在为 Windows Phone 开发一个应用程序,其中的 ListBox 显示来自 JSON 文件的数据。我使用的是 JArray,我可以根据数组位置显示数据。但是,如果我想显示我的 JSON
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 3 年前。 Improve t
我是一名优秀的程序员,十分优秀!