java string conversion integer (ATOI)

2023-01-25   ES  

The previous chapter has briefly introduced how to assert the response value of the interface. In the actual work process, JSON’s response content is often very complicated. Facing the complex JSON response body, it is mainly solved by JSONPATH. JSONPATH provides a powerful JSON parsing function. Using its own Xpath syntax, it can be more convenient and flexible to obtain the corresponding JSON content.

Environmental preparation

pip install jsonpath

<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version>2.6.0</version>
</dependency>

XPath and JSONPATH syntax

below is compared with XPath and JSONPATH syntax. The positioning of the two has a lot of similarities:

[External chain pictures failed, the source station may have a anti-theft chain mechanism. It is recommended to save the picture and upload it directly (IMG-WSZXBEZ2-1659492631446) (upload: //5ovlgiyeqfmwfxkiiq3tu.png)]]

For example, the same field, the grammar in XPath is:

/store/book[0]/title

jsonpath’s syntax is:

$.store.book[0].title
$['store']['book'][0]['title']

Below is a set of JSON structures, which are extracted by JSONPATH and XPath, respectively

The comparison of XPath and JSONPATH in

{
  "store": {
    "book": [
      {
        "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      {
        "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      },
      {
        "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      },
      {
        "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }
    ],
    "bicycle": {
      "color": "red",
      "price": 19.95
    }
  }
}

:

For more content, please visit: https://goessner.net/articleS/jsonPath

actual combat exercises

The following is https://ceshiren.com/t/topic/6950.json’s normal response value (because the response is too long, delete part of the content):

{ 
   'Post_stream': { 
     'Posts': [ 
       {{ 
         'ID': 17126, 
         'name': 'Si Han', 
         'username': 'seveniruby', 
         'Avatar_template': '/User_avata/Ceshiren.com/seveniruby/ {Size }/2_2.png', 
         'Created_at': '2020-10-02t04: 23: 30.586z', 
         'COOKED': '<p> The average salary increase has always been more than 30%. The record of this refresh is estimated to be maintained for several years. </P>', 
         'Post_number': 6, 
         'Post_type': 1, 
         'Updated_at': '2020-10-02t04: 23: 48.775z', 
         'Reply_to_post_number': None, 
         'Reads': 651, 
         'Readers_count': 650, 
         'SCORE': 166.6, 
         'YOURS': False, 
         'Topic_id': 6950, 
         'Topic_slug': 'Topic', 
         'Display_username': 'Si Han ", 
         'Primary_group_name': 'python_12', 
         Omitted ... omitted ... 
       }, 
     ],,, 
   }, 
   'Timeline_lookup':, 
   'Suggestted_topics' :, 
   'tags': [ 
     'The essence of post', 
     'Test development', 
     'Test job search', 
     'Outsourcing test' 
   ],,, 
   'ID': 6950, 
   'Title': 'Test Life | From outsourcing rookie to testing development, salary is tripled a year, and I can't even believe it! (Attach the real questions and answers) ', 
   'Fancy_title': 'Test Life | From outsourcing rookie to testing development, salary is tripled a year, and I dare not believe it! (Attach the real questions and answers) ', 

 }

Next, you need to realize a request. The cooked of the name field in the response content of the above response content is “salary increase”

jsonpath assertion

Import Requests 
 From jsonpath import jsonpath 
 R = Requests.get ("https://ceshiren.com/tpic/6950.json") .json () 
 Result = jsonpath (R, "$ .. Posts [? (@. name == 'Sihan')]. Cooked") [1] 
 Assert "Salary" in Result

jsonpath assertion

Import com.jayway.jsonPath.jsonPath; 
 import org.junit.jupiter.api.test; 
 Import java.util.list; 
 Import Static IO.RESTASSURED.RESTASSSURED.GIVEN; 
 public class jsontest { 

     @Teest 
     void jsontest () { 
         // Get the response information and convert it into a string type 
         String res = given (). When (). 
                 get ("https://ceshiren.com/t/topic/6950.json")) 
                 .then (). Extract (). Response (). ASSTRING (); 
         // Extract the required fields through the JSONPATH expression 
         List <string> result = jsonpath.read (res, "$ .. Posts [? (@. Name == 'Sihan')]. 
         // Ecclail verification 
         assert result.get (1) .contains ("salary increase"); 
     } 
 }

source

Related Posts

POJ 3764 The Xor-Longest Path Title ZWARD

java ~ Introduce the three major features and guardians of the thread

javascript’s thousands of points and remove thousands of points

Question 1022: Cruise rental 2007 Zhejiang University Computer and Software Engineering Graduate Test Test True J

java string conversion integer (ATOI)

Random Posts

FLV file’s RTMP push flow

ORG.Hibernate.MappingException: Dialect Does Not Support Identity Key Generation

Pyecharts Draw Map (Geo and Map) (Rich details) Hello

Handwriting digital picture dual -value converted to 32*32 array.

bind function