반응형

JSONPath 란 무엇입니까?

JSONPath는 JSON 데이터에서 섹션을 찾고 추출하는 경량 라이브러리입니다. JavaScript와 PHP 모두에서 사용할 수 있습니다

 


JSONPath 얻기

JSONPath 및 JavaScript를 사용하려면 jsonpath.js를 다운로드해야 합니다. http://code.google.com/p/jsonpath/에서 다운로드할 수 있습니다.

다운로드한 후에는 해당 js 파일을 웹페이지에 포함해야 하며 사용할 준비가 된 것입니다.

통사론:

jsonPath(obj, expr [, args])
복사

매개변수:

매개변수설명
obj (객체|배열) 이 매개변수는 JSON 구조를 나타내는 개체를 나타냅니다.
expr (문자열) 이 매개변수는 JSONPath 표현식 문자열을 나타냅니다.
인수(객체|정의되지 않음) 이 매개변수는 객체 제어 경로 평가 및 출력을 나타냅니다. 이 글을 쓰는 시점에서는 단 한 명의 멤버만 지원됩니다.
args.resultType("VALUE"|"PATH") 기본적으로 이 매개변수를 사용하면 결과가 일치하는 값이 됩니다. 그렇지 않으면 정규화된 경로 표현식입니다.

반환 값

반환 값설명
정렬 입력으로 제공된 경로 표현식과 일치하는 값 또는 정규화된 경로 표현식으로 구성된 배열입니다.
거짓 일치하는 항목이 없으면 반환됩니다.

 

JavaScript와 함께 JSONPath를 사용하는 예

우리가 작업할 JSON은 다음과 같습니다.

 { "MovieDatabase": {
  "movie": [
         	  { "name":"The Change-Up",
  "genre": "comedy",
  "director": "David Dobkin",
  "Facebook_like": 252
         	  },
         	  { "name":"Rise of the Planet of the Apes",
  "genre": "SciFi",
  "director": "Rupert Wyatt",
  "Facebook_like": 472
         	  },
         	  { "name":"30 Minutes or Less",
  "genre": "adventure",
  "director": "Ruben Fleischer",
  "Facebook_like": 114
         	  },
         	  { "name":"Final Destination 5",
  "genre": "Horror",
  "director": "Steven Quale",
  "Facebook_like": 241
         	  }
         	  ]
         	  }
         	  }
복사

위 JSON 데이터의 다양한 부분을 찾아 추출하는 JavaScript 코드는 다음과 같습니다. (json.js 파서의 메서드를 사용할 것이므로 해당 메서드도 다운로드하여 포함해야 합니다.)

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JavaScript JSONPath example | JSON tutorial | w3resource</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="/JSON/json.js"></script>
<script type="text/javascript" src="/JSON/jsonpath.js"></script>
</head>
<body>
<h1>This is an example of JavaScript with JSONPath</h1>
<script type="text/javascript">
         	  var json = { "MovieDatabase": {
  "movie": [ 
         	  { "name":"The Change-Up",
  "genre": "comedy",
  "director": "David Dobkin",
  "Facebook_like": 252
         	  },
         	  { "name":"Rise of the Planet of the Apes",
  "genre": "SciFi",
  "director": "Rupert Wyatt",
  "Facebook_like": 472
         	  },
         	  { "name":"30 Minutes or Less",
  "genre": "adventure",
  "director": "Ruben Fleischer",
  "Facebook_like": 114
         	  },
         	  { "name":"Final Destination 5",
  "genre": "Horror",
  "director": "Steven Quale",
  "Facebook_like": 241
         	  }
         	  ]
         	  }
         	  };
         	  result = "";
         	  result += jsonPath(json, "$.MovieDatabase.movie[*].director").toJSONString() + "<br />";
         	  //find all directors
         	  result += jsonPath(json, "$..director").toJSONString() + "<br />";
         	  //find all directors
         	  result += jsonPath(json, "$.MovieDatabase.*").toJSONString() + "<br />";
         	  //find all movies
         	  result += jsonPath(json, "$.MovieDatabase..Facebook_like").toJSONString() + "<br />";
         	  //find all facebook lies of all the movies
         	  result += jsonPath(json, "$..movie[(@.length-1)]").toJSONString() + "<br />";
         	  //the last movie in data
         	  result += jsonPath(json, "$..movie[-1:]").toJSONString() + "<br />";
         	  //the last movie in data
         	  result += jsonPath(json, "$..movie[0,1]").toJSONString() + "<br />";
         	  //first two movies
         	  result += jsonPath(json, "$..movie[:3]").toJSONString() + "<br />";
         	  //first three movies
         	  result += jsonPath(json, "$..movie[?(@.genre)]").toJSONString() + "<br />";
         	  //all movies with genre
         	  result += jsonPath(json, "$..movie[?(@.Facebook_like>200)]").toJSONString() + "<br />";
         	  //movies with facebook like more that 200
         	  result += jsonPath(json, "$..*").toJSONString() + "\n";
         	  // all members in the JSON document
         	  document.write(result);
  </script>
  </body>
  </html>
 

 

 

 

 

 

 

출처 : https://www.w3resource.com/JSON/JSONPath-with-PHP.php

반응형

'JSON' 카테고리의 다른 글

온라인 JSON 뷰어  (1) 2023.11.27
JSON의 구조  (1) 2023.11.27
JSON 소개  (1) 2023.11.27

+ Recent posts