【问题标题】:String formulation in JavaJava中的字符串公式
【发布时间】:2016-10-28 11:31:13
【问题描述】:

我需要像这样在 javaME 中编写一个 URL 字符串(这不允许我使用许多可以立即执行此操作的库)

     url = "http://helloworld.com/index.php?data={\"Word\":"+wordX+",\"RS\":20/04/13-2008:31:44,\"SER\":"+net2+"}"; 

这是一个这样的字符串:

http://helloworld.com/index.php?data={"Word":358741051173885,"RS":20/04/13-2008:31:44,"SER":53543d303b44523d4e616fd}

问题是我需要值也包含在逗号内。我已经尝试了一切,但我无法理解如何去做。 有人可以解释一下吗?

谢谢

【问题讨论】:

  • 我需要这些值也包含在逗号内”。请举例说明。
  • 为什么不使用众多 json 库之一呢?例如杰克逊?
  • 你的意思是值也应该被引用吗? http://helloworld.com/index.php?data={"Word":"358741051173885","RS":"20/04/13-2008:31:44","SER":"53543d303b44523d4e616fd"}
  • 我希望这是你所期待的。 String str = "helloworld.com/index.php?data={\"Word\":\"358741051173885\",\"RS\":\"20/04/ 13-2008:31:44\",\"SER\":\"53543d303b44523d4e616fd\"}";
  • 这正是我想要的,但声明变量名 WordX 和 Net2 而不是传递值!

标签: java string http url


【解决方案1】:

像这样?

String url = String.format("http://helloworld.com/index.php?data={\"Word\":\"%s\",\"RS\":20/04/13-2008:31:44,\"SER\":\"%s\"}", wordX, net2);

【讨论】:

    【解决方案2】:

    创建MessageFormat

    final MessageFormat urlFormat = new MessageFormat("http://helloworld.com/index.php?data={\"Word\":\"{0}\",\"RS\":20/04/13-2008:31:44,\"SER\":\"{1}\"}");
    

    然后只需将您的值应用于格式:

    final String url = urlFormat.format(new Object[]{wordX, net2});
    

    或者,创建一个格式String

    final String urlFormat = "http://helloworld.com/index.php?data={\"Word\":\"%s\",\"RS\":20/04/13-2008:31:44,\"SER\":\"%s\"}";
    

    并使用String.format:

    final String url = String.format(urlFormat, wordX, net2);
    

    或者,只需将您的 String 更改为:

    url = "http://helloworld.com/index.php?data={\"Word\":\""+wordX+"\",\"RS\":20/04/13-2008:31:44,\"SER\":\""+net2+"\"}"; 
    

    【讨论】:

    • 我不能这样做,因为这是针对 javaME 项目的,这意味着不使用 1.3 jre 以上的任何东西。我忘了在问题中提到它。我的错!
    • @kohhworlwide 你不觉得有一个重大的遗漏吗?!
    • 确实如此!哈哈
    • @kohhworlwide 我添加了一个最终选项,它应该适合你。
    • 完美!谢谢鲍里斯!万圣节快乐哈哈。
    【解决方案3】:
    String val1 = "";
        String val2 = "";
    
        String str =    "http://helloworld.com/index.php?data={\"Word\":\""+val1+"\",\"RS\":\"20/04/13-2008:31:44\",\"SER\":\""+val2+"\"}";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-14
      • 2021-10-31
      • 2010-10-26
      • 2011-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多