简体   繁体   English

按字母顺序对JSON输出进行排序

[英]Sort JSON Output Alphabetically

I am after help on how to write/modify the below code so that my JSON output is sorted alphabetically by the 'Currency' field. 我正在寻求有关如何编写/修改以下代码的帮助,以便使我的JSON输出通过“ Currency”字段按字母顺序排序。

Here is the code that performs the output of the JSON:- 以下是执行JSON输出的代码:-

  def balance = Action(parse.json) { implicit request =>
    val body = request.body
    (for (
      apiKey <- (body \ "api_key").validate[String]
    ) yield {
      val balances = globals.engineModel.balance(None, Some(apiKey))
      Ok(Json.toJson(balances.map({ c =>
        Json.obj(
          "currency" -> c._1,
          "amount" -> c._2._1.bigDecimal.toPlainString,
          "hold" -> c._2._2.bigDecimal.toPlainString
        )
      })
      ))
    }).getOrElse(
      BadRequest(Json.obj("message" -> Messages("messages.api.error.failedtoparseinput")))
    )
  }

And the second part of the code is:- 代码的第二部分是:

    function show_balance(){
        API.balance().success(function(balances){
            for (var i = 0; i < balances.length; i++) {
                balances[i].available = zerosToSpaces(Number(balances[i].amount) - Number(balances[i].hold));
                balances[i].amount = zerosToSpaces(balances[i].amount);
                balances[i].hold = zerosToSpaces(balances[i].hold);
            }
            $('#balance').html(template(balances));
        });
    }
    show_balance();

This is tagged JavaScript, but looks more like Scala. 这被标记为JavaScript,但看起来更像Scala。 Assuming balance is a type with a deterministic order (not a Set ), you should be able to do balance.sortBy(_._1) . 假设balance是具有确定性顺序的类型(而不是Set ),则您应该能够做balance.sortBy(_._1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM