0
[
{"fname":"Foo","lname":"Pacman"},
{"fname":"Bar","lname":"Mario"},
{"fname":"Poo","lname":"Wario"}
]

Well I have JSON string in this format, Now what I need is to convert each tuples -> {"fname":"Foo","lname":"Pacman"}

To a Person object, for e.g. lets assume I have a case class

case class Person(fname:String,lname:String)

Now how am I to get, List<person>

If I had a JSON containing data for single tuple, then I could,

val o:Person = parse[Person](jsonString)// I am actually using Jerkson Lib

But since there are more than one tuples, how am i to parse them individually and create objects and create a list.


Replace standard javascript confirm with Twitter Bootstrap's modal - who triggered it?

I'm trying to replace the standard javascript confirm with a twitter bootstrap modal window. Everything is almost working (the modal is shown with its confirm text), but I'm stuck trying to catch the caller href, which I need to bind the "ok" button.

Here's my code (almost working example: http://jsfiddle.net/k5uDw/):

jQuery.altConfirm = function (options) {
    var box = '<div class="modal fade static" id="confirm" data-backdrop="static" tabindex="-1" role="dialog">';
        box += '<div class="modal-dialog">';
            box += '<div class="modal-content">';
                box += '<div class="modal-body"> </div>';
                box += '<div class="modal-footer">';
                    box += '<button type="button" class="btn btn-default" data-dismiss="modal">No</button>';
                    box += '<button type="button" class="btn btn-primary">Ok</button>';
                box += '</div>';
            box += '</div>';
        box += '</div>';
    box += '</div>';
    $("body").append(box);

    window.confirm = function () {
        $(".modal-body").html( arguments[0].replace(/\n/, "<br />") );
        $('.modal').modal();
        $(".btn-default").on('click', function() {
            $(this).modal('hide');
        });
        $(".btn-primary").on('click', function() {
            return true; // this is where I need the caller's HREF, 
                         // to make it actually proceed.
                         // Return true, obviously, does nothing.
        });
    };
};

$(document).ready(function() {
    $.altConfirm();
});

Any hint? Please note that I would like this to be a drop-in replacement for standard javascript confirm, so modifying the way the confirm itself is called it is not a possibility (if this plugin is active -> then the modal is shown, if this plugin is not active -> then the standard confirm is fired).

4

2 回答 2

2

Jerkson 支持开箱即用的反序列化对象列表,因此您需要做的就是:

val people = parse[List[Person]](personJson)
于 2014-06-10T12:44:17.037 回答
2

您可以使用json4s(它是 jackson 或 lift-json 的包装器),您还可以在其中获得开箱即用的解析功能。

   import org.json4s._
   import org.json4s.jackson.JsonMethods._
   implicit val formats = DefaultFormats 

    val personJson = """
      [
      {"fname":"Foo","lname":"Pacman"},
      {"fname":"Bar","lname":"Mario"},
      {"fname":"Poo","lname":"Wario"}
      ]"""
    case class Person(fname:String,lname:String)
    val people = parse(personJson).extract[List[Person]]
于 2014-06-10T12:52:32.977 回答