Open Source RDBMS - Seamless, Scalable, Stable and Free

English | Login |Register

How to parse JSON


nGrinder 3.1.1 contains json.jar, one of most famous Json parser(provided from www.json.org). You can easily parse json string with this library.
You can use this like the following.

JYTHON

from org.json import JSONObject 

message = """ 
{ 
  "glossary": { 
     "title": "example glossary", 
        "GlossDiv": { 
           "title": "S", 
           "GlossList": { 
              "GlossEntry": { 
                 "ID": "SGML", 
                 "SortAs": "SGML", 
                 "GlossTerm": "Standard Generalized Markup Language", 
                 "Acronym": "SGML", 
                 "Abbrev": "ISO 8879:1986", 
                 "GlossDef": { 
                    "para": "A meta-markup language, used to create markup languages such as DocBook."
                 }, 
                 "GlossSee": "markup" 
            } 
         } 
      } 
  } 
} 
""" 

json = JSONObject(message) 
grinder.logger.info(json.get("glossary").getString("title")) 

GROOVY

For groovy you can use JsonSlurper instead.
import groovy.json.JsonSlurper


....
def message = """ 
{ 
  "glossary": { 
     "title": "example glossary", 
        "GlossDiv": { 
           "title": "S", 
           "GlossList": { 
              "GlossEntry": { 
                 "ID": "SGML", 
                 "SortAs": "SGML", 
                 "GlossTerm": "Standard Generalized Markup Language", 
                 "Acronym": "SGML", 
                 "Abbrev": "ISO 8879:1986", 
                 "GlossDef": { 
                    "para": "A meta-markup language, used to create markup languages such as DocBook."
                 }, 
                 "GlossSee": "markup" 
            } 
         } 
      } 
  } 
}"""
def jsonMsg = new JsonSlurper().parseText(message)
grinder.logger.info(jsonMsg.glossary.title)
comments powered by Disqus
문서 정보
viewed 2067 times
번역 en
작성자
posted 작년
junoyoon
공헌자
마지막 수정시간 3달 전
변경 내역 보기
Share this article