简体   繁体   中英

What is the Best way to find out the information that match with a given line number

I have a JSON with lots of arrays inside that. I Need the best way to extract information from it. The thing is need the best way to do that otherwise my suggestion have to wait lots of time give the output. Is there a good way to do that without doing iterations over every JSON array. I attached a sample JSON Data set that i am getting.

  "program": [
    {
      "sourceElements": [
        {
          "sourceElement": [
            {
              "statement": []
            }
          ]
        },
        {
          "sourceElement": [
            {
              "statement": [
                {
                  "line": 1,
                  "column": 0,
                  "type": "Function",
                  "text": "function"
                }
              ]
            }
          ]
        },
        {
          "sourceElement": [
            {
              "statement": [
                {
                  "expressionStatement": [
                    {
                      "expressionSequence": [
                        {
                          "argumentsExpression": [
                            {
                              "identifierExpression": [
                                {
                                  "line": 1,
                                  "column": 9,
                                  "type": "Identifier",
                                  "text": "a"
                                }
                              ]
                            },
                            {
                              "arguments": [
                                {
                                  "line": 1,
                                  "column": 11,
                                  "type": "OpenParen",
                                  "text": "("
                                },
                                {
                                  "line": 1,
                                  "column": 12,
                                  "type": "CloseParen",
                                  "text": ")"
                                }
                              ]
                            }
                          ]
                        }
                      ]
                    },
                    {
                      "eos": []
                    }
                  ]
                }
              ]
            }
          ]
        },
        {
          "sourceElement": [
            {
              "statement": [
                {
                  "block": [
                    {
                      "line": 1,
                      "column": 14,
                      "type": "OpenBrace",
                      "text": "{"
                    }
}

    }
  ]
} ```

You can use Jayway JsonPath (A Java DSL for reading JSON documents.Jayway JsonPath).

You can extract elements that you want from JSON with json-path expression.

eg:

$.store.book[*].author  The authors of all books

$..author   All authors

$.store.*   All things, both books and bicycles

$.store..price  The price of everything

$..book[2:] Book number two from tail

$..book[?(@.isbn)]  All books with an ISBN number

$.store.book[?(@.price < 10)]   All books in store cheaper than 10

$..book[?(@.price <= $['expensive'])]   All books in store that are not "expensive"

$..book[?(@.author =~ /.*REES/i)]   All books matching regex (ignore case)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM