【问题标题】:Getting an exception: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected ':' at line 1 column 24获得异常:com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected ':' at line 1 column 24
【发布时间】:2014-11-27 05:58:38
【问题描述】:

请不要将其标记为重复,我浏览了许多相关的帖子,但都是徒劳的。我是 Android/Java 编程的新手,需要帮助了解如何将 json 映射转换为 Java 哈希映射。请参考以下代码:

HttpPost httpPost1 = new HttpPost(
                        "http://www.mywebsite.com/something.json" );
                Log.i("MAP", "Hash call");
                List<NameValuePair> nameValuePair1 = new ArrayList<NameValuePair>(1);
                nameValuePair1.add(new BasicNameValuePair("id_token", token ));

                    try {
                        httpPost1.setEntity(new UrlEncodedFormEntity(nameValuePair1));
                    } catch (UnsupportedEncodingException e) {
                        // writing error to Log
                        e.printStackTrace();
                    }

                    // Making HTTP Request
                    try {
                    HttpResponse response1 = httpClient.execute(httpPost1);
                    BufferedReader reader = new BufferedReader(new InputStreamReader(response1.getEntity().getContent(), "UTF-8"));
                   StringBuilder builder = new StringBuilder();
                   for (line = null; (line = reader.readLine()) != null;) {
                      // builder.append(line).append("\n");

                       System.out.println(line);
                     Log.i("Line1", "line1 "+ line);

                 class Data {
                   private Email email;
                   private String result;
                   // Add/generate getters and setters.

                   class Email {
                       private List<Alert> alerts;
                       private String session;
                       // Add/generate getters and setters.
                   }

                   class Alert {
                       private Long AlertID;
                       private String tim;
                       private String lng;
                       private String lat;

                       }
               }

             Gson gson=new Gson(); 
             String json="{\"AlertID\":\"tim\",\"lng\",\"lat\"}";
             Map<String,String> map=new HashMap<String,String>();
            java.lang.reflect.Type type = new TypeToken<Map<String, String>>(){}.getType();
            Map<String, String> myMap = gson.fromJson("{\"AlertID\":\"tim\",\"lng\",\"lat\"}", type);
             System.out.println(json);
            Log.i("JAVA Hash", "Java Hash call");
                   }//Closing for loop//line


                   JSONTokener tokener = new JSONTokener(builder.toString());
                   JSONArray finalResult = new JSONArray(tokener);
                        // writing response to log
                    Log.i("Http Response:", response1.toString());
                    Log.i("JSON returns: ", "JSON returns");
                    } catch (ClientProtocolException e) {
                        // writing exception to log
                        e.printStackTrace();
                    } catch (IOException e) {
                        // writing exception to log
                        e.printStackTrace();

                    }

需要转成hashmap的json如下:

 {\"myemailid@gmail.com\":\"lat\":0.0,\"lng\":0.0,\"tim\":"2014-10-02T10:38:11.437Z"},{"lat":0.0,"lng":0.0,"tim":"2014-10-02T08:56:38.459Z"},{"lat":0.0,"lng":0.0,"tim":"2014-10-02T08:47:18.713Z"},{"lat":0.0,"lng":0.0,"tim":"2014-10-02T08:43:58.145Z"},{"lat":0.0,"lng":0.0,"tim":"2014-10-02T08:34:08.798Z"}]}

只是为了让 json 更具可读性:

myemailid@gmail.com

纬度:0 液化天然气:0 蒂姆:“2014-10-01T16:55:15.002Z”

纬度:0 液化天然气:0 蒂姆:“2014-10-01T16:18:07.290Z”

纬度:0 液化天然气:0 蒂姆:“2014-10-01T12:04:06.364Z”

纬度:0 液化天然气:0 蒂姆:“2014-10-01T11:58:04.455Z”

纬度:0 液化天然气:0 蒂姆:“2014-10-01T11:46:24.560Z”

在这方面的任何帮助将不胜感激。

【问题讨论】:

    标签: java android json map hashmap


    【解决方案1】:

    您是否尝试将" 放在您的数值周围?

    {"email":"myemailid@gmail.com",
     "items":[{"lat":"0.0","lng":"0.0","tim":"2014-10-02T08:56:38.459Z"},
              {"lat":"0.0","lng":"0.0","tim":"2014-10-02T08:47:18.713Z"},
              {"lat":"0.0","lng":"0.0","tim":"2014-10-02T08:43:58.145Z"},
              {"lat":"0.0","lng":"0.0","tim":"2014-10-02T08:34:08.798Z"},
              {"lat":"0.0","lng":"0.0","tim":"2014-10-02T08:28:21.437Z"}]}
    

    曾经遇到过缺少引号的问题......如果它与 gsongenson. 相关,则不再 100% 确定

    添加:上面也添加了"email""items"

    Gson gson = new GsonBuilder().create();
    ResultJson result = gson.fromJson(jsonStringAsShownAbove, ResultJson.class); 
    
    class ResultJson {
        private String email;
        private List<TimeAndLocation> items = new ArrayList<TimeAndLocation>();
    }
    
    class TimeAndLocation {
        private double lat;
        private double lon;
        private String tim;
    }
    

    【讨论】:

    • 我试过了,但没有帮助。您能否建议我应该如何将给出的 json 转换为 java 哈希?
    • 我认为代码看起来如上所示。请注意我对 JSON 结构的更新。如果您的第一个标识符是电子邮件地址,我预计会出现一些问题,因此我将其更改为 "email"
    【解决方案2】:

    您收到此异常的原因是您没有使用您在问题中发布的 JSON。

    您使用的是 JSON:

    {\"AlertID\":\"tim\",\"lng\",\"lat\"}
    

    您在代码的以下行中使用了它:

     Map<String, String> myMap = gson.fromJson("{\"AlertID\":\"tim\",\"lng\",\"lat\"}", type);
    

    上面的 JSON 显然是格式错误的 JSON,因为属性 lng 没有与之对应的值,而有效的 JSON 必须具有该值。所以,理想情况下,你的 JSON 应该是这样的:

    {\"AlertID\":\"tim\",\"lng\":0.0,\"lat\":0.0}
    

    尝试使用格式正确的实际 JSON,您的问题应该会得到解决。

    【讨论】:

    • 我按照你的指示试过了,但是得到了这样的结果: {"AlertID":"tim","lng":0.0,"lat":0.0} 我需要存储整个 json作为一个 java 哈希,所以我可以在需要的时候访问它。整个 json 看起来像: JSON myemailid@gmail.com 0 lat : 0 lng : 0 tim : "2014-10-01T16:55:15.002Z" 1 lat : 0 lng : 0 tim : "2014-10-01T16:18 :07.290Z" 2 纬度: 0 lng:0 时: "2014-10-01T12:04:06.364Z" 3 时:0 lng:0 时:"2014-10-01T11:58:04.455Z" 4 时:0 lng : 0 tim : "2014-10-01T11:46:24.560Z" 请告诉我如何将其转换为 java 哈希。非常感谢。
    • 已更新。请进一步提出建议。
    • myemailid@gmail.com 到底是什么。我假设它是键,其值为Alert 的数组。此外,以有效格式发布 JSON。
    • 我正在对一个网站进行 POST 调用,该网站返回一个 json 哈希(如问题中所发布),其中包含电子邮件 id 作为键和 5 个最后坐标(最新的第一个)作为元素的数组钥匙。我现在需要将它存储为 java 哈希。从网站返回的确切 json 是: {"myemailid@gmail.com":[{"lat":0.0,"lng":0.0,"tim":"2014-10-01T16:55:15.002Z" },{"lat":0.0,"lng":0.0,"tim":"2014-10-01T16:18:07.290Z"},{"lat":0.0,"lng":0.0,"tim": "2014-10-01T12:04:06.364Z"},{"lat":0.0,"lng":0.0,"tim":"2014-10-01T11:58:04.455Z"},{"lat": 0.0,"lng":0.0,"tim":"2014-10-01T11:46:24.560Z"}]}
    • 密钥总是myemailid@gmail.com 吗?或者有什么不同?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-11
    • 1970-01-01
    相关资源
    最近更新 更多