php解析json里的hson_json在haskell中解析

I'm trying to parse JSON data in haskell. Having gone through a slew of websites, this is the furthest I have been able to get to.

data Address = Address { house :: Integer, street :: String, city :: String, state :: String, zip :: Integer } deriving (Show)

data Person = Person { name :: String, age :: Integer, address :: Address } deriving (Show)

getName :: Person -> String

getName (Person n _ _) = n

getAddress :: Person -> Address

getAddress (Person _ _ a) = a

getState :: Address -> String

getState (Address _ _ _ s _) = s

I write that in a file ex.hs and load it in ghci -->

Prelude> import Text.JSON

Prelude Text.JSON> :load ex

Main Text.JSON> let aa = "{\"name\": \"some body\", \"age\" : 23, \"address\" : {\"house\" : 285, \"street\" : \"7th Ave.\", \"city\" : \"New York\", \"state\" : \"New York\", \"zip\" : 10001}}"

...> decode aa :: Result JSValue

It returns

Ok (JSObject (JSONObject {fromJSObject = [("name",JSString (JSONString {fromJSString = "some body"})),("age",JSRational False (23 % 1)),("address",JSObject (JSONObject {fromJSObject = [("house",JSRational False (285 % 1)),("street",JSString (JSONString {fromJSString = "7th Ave."})),("city",JSString (JSONString {fromJSString = "New York"})),("state",JSString (JSONString {fromJSString = "New York"})),("zip",JSRational False (10001 % 1))]}))]}))

Needless to say, it seems pretty verbose (and frightening). I tried doing

...> decode aa :: Result Person

and it gave me an error. How do I go about populating an instance of the Person datastructure from this json string? For example, what should I do to get the state of the person in the JSON string...

解决方案

The problem is that Text.JSON does not know how to convert JSON data to

your Person data type. To do this, you need to either make Person and

instance of the JSON typeclass, or your can use Text.JSON.Generic and the

DeriveDataTypeable extension to do the work for you.

Generics

The Text.JSON.Generic method will read the JSON structure based on the

structure of your data type.

{-# LANGUAGE DeriveDataTypeable #-}

import Text.JSON.Generic

data Address = Address

{ house :: Integer

, street :: String

, city :: String

, state :: String

, zip :: Integer

} deriving (Show, Data, Typeable)

data Person = Person

{ name :: String

, age :: Integer

, address :: Address

} deriving (Show, Data, Typeable)

aa :: String

aa = "{\"name\": \"some body\", \"age\" : 23, \"address\" : {\"house\" : 285, \"street\" : \"7th Ave.\", \"city\" : \"New York\", \"state\" : \"New York\", \"zip\" : 10001}}"

main = print (decodeJSON aa :: Person)

This method works really well as long as you don't mind matching the names of the fields

in your data structure to your JSON format.

As an aside, you don't need to write functions like getName, getAddress,

and getState. The names of the field in your record type are accesor

functions.

∀ x. x ⊦ :t house

house :: Address -> Integer

∀ x. x ⊦ :t address

address :: Person -> Address

JSON Instance

Alternatively, you could take the high road and implement your own instance of

the JSON class.

import Control.Applicative

import Control.Monad

import Text.JSON

data Address = Address

{ house :: Integer

, street :: String

, city :: String

, state :: String

-- Renamed so as not to conflict with zip from Prelude

, zipC :: Integer

} deriving (Show)

data Person = Person

{ name :: String

, age :: Integer

, address :: Address

} deriving (Show)

aa :: String

aa = "{\"name\": \"some body\", \"age\" : 23, \"address\" : {\"house\" : 285, \"street\" : \"7th Ave.\", \"city\" : \"New York\", \"state\" : \"New York\", \"zip\" : 10001}}"

-- For convenience

(!) :: (JSON a) => JSObject JSValue -> String -> Result a

(!) = flip valFromObj

instance JSON Address where

-- Keep the compiler quiet

showJSON = undefined

readJSON (JSObject obj) =

Address

obj ! "house"

obj ! "street"

obj ! "city"

obj ! "state"

obj ! "zip"

readJSON _ = mzero

instance JSON Person where

-- Keep the compiler quiet

showJSON = undefined

readJSON (JSObject obj) =

Person

obj ! "name"

obj ! "age"

obj ! "address"

readJSON _ = mzero

main = print (decode aa :: Result Person)

This takes advantage of the fact that the Result type is an Applicative to easily

chain together queries on the JSObject value.

This is a little more work, but it gives you more control of the structure of

the JSON if you have to deal with JSON that will cause style guideline

violations due to weird field names.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值