[英]What is an elegant way to parse for a specific part of a JSON string using a different key:value pair? (Scala)
I need to find an Id based on it's associated name. 我需要根据其关联名称找到一个ID。 My program makes REST calls to an API. 我的程序对API进行REST调用。 The API returns results in JSON format. API以JSON格式返回结果。 The Name is unique, so I would like to use it to get it's Id value. 名称是唯一的,因此我想使用它来获取其ID值。 Note the ... can contain anything and does include some {Such}Id keys. 请注意,...可以包含任何内容,并且确实包含一些{Such} Id键。 The Id's can be nested in any number of {...{...{...}...}...} The Id's are always immediately before the name. ID可以嵌套在任意数量的{... {... {...} ...} ...}中。ID始终紧靠名称之前。
Note: ... is code that for privacy reasons cannot be shown. 注意:...是出于隐私原因无法显示的代码。 The code itself(before exclusion of private data) is the result of a REST call as returned by Advanced Rest Client and verified at http://jsonlint.com/ to be valid JSON. 代码本身(在排除私有数据之前)是由Advanced Rest Client返回并在http://jsonlint.com/上验证为有效JSON的REST调用的结果。
The code is returned as such: 代码返回如下:
{
Id: "d5a94d1a-afb7-4e1d-ae0d-a22e01393666"
ProjectId: "ed61c45a-f208-4115-8584-a21a00c51ac0"
Name: "Automated Runs"
OrderNumber: 0
Expands: [3]
0: "Children"
1: "Parent"
2: "Project"
...
scripts: [4]
0: {
Id: "0b70a55c-5e68-4b27-bfcf-a22f00c5dc48"
Name: "3816"
PackageId: "d5a94d1a-afb7-4e1d-ae0d-a22e01393666"
ProjectId: "ed61c45a-f208-4115-8584-a21a00c51ac0"
...
Expands: [6]
0: "Assignments"
1: "Attachments"
2: "FieldControls"
3: "FieldValues"
4: "Package"
5: "Steps"
...
1: {
Id: "14e5c663-0d5a-46bb-ac48-a22f00c15998"
Name: "3814"
PackageId: "d5a94d1a-afb7-4e1d-ae0d-a22e01393666"
ProjectId: "ed61c45a-f208-4115-8584-a21a00c51ac0"
...
Expands: [6]
0: "Assignments"
1: "Attachments"
2: "FieldControls"
3: "FieldValues"
4: "Package"
5: "Steps"
...
2: {
Id: "00d52fcd-b611-4f69-aeb6-a22f00c263a9"
Name: "3815"
ProjectId: "ed61c45a-f208-4115-8584-a21a00c51ac0"
...
Expands: [6]
0: "Assignments"
1: "Attachments"
2: "FieldControls"
3: "FieldValues"
4: "Package"
5: "Steps"
...
3: {
Id: "4d3a6132-8497-4b6b-a064-a22f00c669ff"
Name: "3817"
...
Expands: [6]
0: "Assignments"
1: "Attachments"
2: "FieldControls"
3: "FieldValues"
4: "Package"
5: "Steps"
...
}
Things I have tried include regex (I'm new to it and having some troubles), and simple string splitting. 我尝试过的东西包括正则表达式(我是新手,遇到了一些麻烦)和简单的字符串拆分。 While I have the string splitting working, it is semi hard coded. 当我进行字符串拆分时,它是半硬编码的。
What i would like is something like: 我想要的是这样的:
def getID(myJSON:String, myName:String){
val pattern = "\"Id\": \"*\",\r\n\"Name\":\"" + myName + "\",\""
get the id (*) from result using pattern
}
Or Even better convert it to be generic. 甚至更好地将其转换为通用。
def getID(myJSON:String, myValue:String, searchKey:String, findKey:String){
val pattern = { ... findKey: *...} in the inner most { ... searchKey: * ...} scope
get the id (*) from result using the pattern in the found {...searchKey...} scope
}
Either would be great and very much appreciated. 两者都会很棒,并且非常感谢。 My current code looks like: 我当前的代码如下:
result.split("Id\": \"")(3).split("\"")(0)
It might be pretty, but it has lots of room for mishaps. 它可能很漂亮,但仍有很多发生意外的空间。 An Id might be created by a user which sets the count to be incorrect, etc... 用户可能创建了一个ID,将计数设置为不正确,等等。
Thank you, Erick Stone 谢谢你,埃里克·斯通
How about using the for comprehension in json4s 如何在json4s中使用for理解
scala> :paste
// Entering paste mode (ctrl-D to finish)
import org.json4s._
import org.json4s.native.JsonMethods._
val json = """
{
"a": {
"Id": "1",
"Name": "Name1",
"b": {
"Id": "2",
"Name": "Name2",
}
}
}
"""
def getId(json: String, name: String) = {
val res = for {
JObject(child) <- parse(json)
JField("Name", JString(n)) <- child
JField("Id", JString(id)) <- child
if n == name
} yield id
res.headOption
}
// Exiting paste mode, now interpreting.
scala> getId(json, "Name1")
res4: Option[String] = Some(1)
scala> getId(json, "Name2")
res5: Option[String] = Some(2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.