【问题标题】:split cookie and return json array拆分cookie并返回json数组
【发布时间】:2016-11-18 00:29:40
【问题描述】:

我正在用 java 制作一个小应用程序。我正在尝试返回 JSON 数组。但我遇到了错误,我需要使用我存储的 cookie,将其拆分并作为 JSON 数组返回。

            String json = null; 
            if(json == null)
            {
                //return empty array
                json = "[]";
            }
            else
            {
               //split cookie
               String wl[] = wishList.split("~");

                json += "[";
                   for(int i = 0; i < json.length(); ++i)
                   {
                      json += wl[i];
                   }
                json += "]"; 

问题是我不确定这是否是返回 json 数组的正确方法, 任何帮助都会很棒。谢谢!

【问题讨论】:

  • 你遇到了什么错误?
  • 我想你的意思是i &lt; wl.length。此外,您需要转义、引用和逗号分隔标记。如果你使用像 jackson 这样的库会容易得多。

标签: java json parsing cookies


【解决方案1】:

我认为你的程序永远不会遇到else statement 如下代码:

 String json = null; 
 if(json == null)
 {
    //return empty array
   json = "[]";
 }

json 会一直"[]",请再检查一遍。

【讨论】:

    猜你喜欢
    • 2019-06-13
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 2022-01-23
    • 2018-12-10
    • 2014-03-13
    • 1970-01-01
    相关资源
    最近更新 更多