杰克逊反序列化json对象的数组,忽略了一些对象

时间:2015-12-28 09:08:18

标签: java json jackson json-deserialization

假设我有这个JSON数组

import UIKit

class ViewController: UIViewController, UITextFieldDelegate, NSXMLParserDelegate, NSURLSessionDataDelegate, NSURLSessionDelegate {

    var MutableData: NSMutableData = NSMutableData()
    var currentElementName: NSString = ""

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let soapMessage = "<?xml version='1.0' encoding='UTF-8'?>..."

//        let session = NSURLSession.sharedSession()


        let urlString = "https://ip:port/"

        let url = NSURL(string: urlString)
        let theRequest = NSMutableURLRequest(URL: url!)
        let msgLength = String(soapMessage.characters.count)

        theRequest.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
        theRequest.addValue(msgLength, forHTTPHeaderField: "Content-Length")
        theRequest.addValue("urn:ResponseAction", forHTTPHeaderField: "SOAPAction")
        theRequest.HTTPMethod = "POST"
        theRequest.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
        let config = NSURLSessionConfiguration.defaultSessionConfiguration()
        let session = NSURLSession(configuration: config, delegate: self, delegateQueue: NSOperationQueue.mainQueue())
        let task = session.dataTaskWithRequest(theRequest)
        task.resume()
    }

    func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void) {

        print("Am in NSURLSessionDelegate didReceiveChallenge")

        if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust && challenge.protectionSpace.host == "example.com" {
            NSLog("yep authorised")
            let credential = NSURLCredential(trust: challenge.protectionSpace.serverTrust!)
            challenge.sender!.useCredential(credential, forAuthenticationChallenge: challenge)
        } else {
            NSLog("nope")
            challenge.sender!.performDefaultHandlingForAuthenticationChallenge!(challenge)
        }

    }
    func URLSessionDidFinishEventsForBackgroundURLSession(session: NSURLSession) {
        print("Am in URLSessionDidFinishEventsForBackgroundURLSession")
        let xmlParser = NSXMLParser(data: MutableData)
        xmlParser.delegate = self
        xmlParser.parse()
        xmlParser.shouldResolveExternalEntities = true
    }
    func URLSession(session: NSURLSession, didBecomeInvalidWithError error: NSError?) {
        print("error of \(error)")
    }

    func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler: (NSURLSessionResponseDisposition) -> Void) {
        print("Am in didReceiveResponse")
        MutableData.length = 0
    }


    func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
        print("Am in didReceiveData")
        MutableData.appendData(data)
    }

    func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
        print("error of \(error)")
    }

}

我有这些Java对象

[{"type":"type1", "id":"1", "name":"John"},
 {"type":"type1", "id":"2", "name":"Jane", 
 {"type":"type2", "id":"3", "name":"Joseph"}]

有没有办法对JSON数组进行反序列化,以便只包含类型为type1的那些?那些有type2的人不应该被设置为null,而是完全被忽略。

1 个答案:

答案 0 :(得分:1)

我认为您可以使用Jackson Filter

实现这一目标