有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何解析postmancollection?

目标

我试图解析postman_echo集合json,并将结果持久化到磁盘上的一个新json副本中,生成与原始文件相同的文件

我更喜欢该语言中的内置数据结构,但使用json库也应该不错。不确定Antlr4是否是更好的方法

后续问题
在post请求主体中是否允许任何有效的嵌套json

更新: https://github.com/chakpongchung/postman-parser 最后我们提出了这个令人满意的解决方案


共 (3) 个答案

  1. # 1 楼答案

    zoran提到的另一种方法是,如果结构不是太动态(使用Play JSON),则创建一个case类。这将使比较结果更容易

    case class MyObject(
      queryString: List[KeyValue],
      method: String,
      url: String,
      httpVersion: String
    ) ... and so on
    
    object MyObject {
        implicit val format: Format[MyObject] = Json.format
    }
    
    case class KeyValue(name: String, value: String)
    
    object KeyValue {
        implicit val format: Format[KeyValue] = Json.format
    }
    

    然后,您只需执行以下操作:

    object JsonParser extends App {
      val postman_collections = "./scala_input.json"
      val jsonifiedString = scala.io.Source.fromFile(postman_collections).mkString
    
      val myJsonData = Try(Json.parse(jsonifiedString)).map(_.as[MyObject])
    
      myJsonData match {
        case Success(myValue) => // compare your case class here
        case Failure(err) => println("none")
      }
    }
    
  2. # 2 楼答案

    解析器中存在多个问题,其中大多数问题是您试图使用默认解析器将Json对象作为字符串处理。例如,在请求中,您将头作为Seq[String]处理,而实际上它是(键、值)对的Seq。对于这种特殊情况,您应该将其更改为以下内容:

    case class Request(
                    method: String,
                    header: Seq[HeaderItem], // header: []
                    url: Option[Url] = None,
                    description: String = ""
                  )
    object Request {
       implicit val format: Format[Request] = Json.using[Json.WithDefaultValues].format
    
    case class HeaderItem(key: String, value: String)
    
    object HeaderItem {
       implicit val format: Format[HeaderItem] = Json.format
     }
    

    如果需要,可以将标题转换为Seq[String],但必须为此编写自定义读取。 在上述情况下,还存在缺少描述的情况,因此您希望使用默认值处理该情况。 在其他一些地方,你也有这样的问题需要处理,例如“回应”

    我注意到的另一个问题是如何处理Json字符串中的“type”属性。Type是保留关键字,您可以通过将其包装在``中来处理它,例如

    case class Script(
        `type`: String,
         exec: Seq[String]
    )
    
  3. # 3 楼答案

    我不确定我是否理解您的问题,但如果您试图迭代json字符串,您可能会尝试以下方法:

      import play.api.libs.json.{JsObject, JsValue, Json}
      import scala.util.{Failure, Success, Try}
    
    
      object JsonParser extends App {
        val postman_coolections = "./resources/scala_input.json"
        val jsonifiedString = scala.io.Source.fromFile(postman_coolections).mkString
    
        val json: JsValue = Try(Json.parse(jsonifiedString)) match {
          case Success(js) => js
          case Failure(ex) => throw new Exception("Couldn't parse json", ex)
       }
     json.asInstanceOf[JsObject].fields.foreach{
       case (key: String, value: JsValue)=>
          println(s"Key:$key value:${value.toString}")
          writeFile(s"$key.json", Json.prettyPrint(value))
     }
    
    //writing the whole postman input as a single file
    
    writeFile("postmanInputFormatted.json", Json.prettyPrint(json))
    writeFile("postmanInput.json", Json.stringify(json))
    
    // To access individual property one option is to use this approach
    
    val lookedValue = json \ "postData" \ "params" \ 1 \ "hello" \ "test"
    
    lookedValue match {
      case JsDefined(value) => println(s"Test value is $value")
      case JsUndefined() => println("Didn't find test value")
    }
    // or
    val lookedValueAlt = (json \ "postData" \ "params" \ 1 \ "hello" \ "test").getOrElse(throw SomeException)