【问题标题】:Gson parsing string that has no key-value pairsGson解析没有键值对的字符串
【发布时间】:2013-08-30 05:02:19
【问题描述】:

我正在尝试使用 Gson 库解析字符串,但没有成功。这是我的字符串:

[["-1.816513","52.5487566"],["-1.8164913","52.548824"]]

这个例子的问题是没有键值对。我查看了其他示例,但它们都有键值对,看起来不像我的问题。

【问题讨论】:

标签: java string parsing gson


【解决方案1】:

我解析字符串列表的解决方案。

package stackoverflow.answers;

import java.lang.reflect.Type;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class GsonTest {

  public static void main(String[] arg) {

    Gson gson = new Gson();
    String jsonOutput = "[[\"-1.816513\",\"52.5487566\"],[\"-1.8164913\",\"52.548824\"]]";
    Type listType = new TypeToken<List<List<String>>>() {}.getType();
    List<List<String>> strings = (List<List<String>>) gson.fromJson(jsonOutput, listType);
    for(List<String> inner: strings){
      for(String s: inner){
        System.out.println(s);
      }
    }

  }
}

但由于值也可以“认为”为双精度值,因此您可以将它们直接解析为解决方案:

package stackoverflow.answers;

import java.lang.reflect.Type;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class GsonTest {

  public static void main(String[] arg) {

    Gson gson = new Gson();
    String jsonOutput = "[[\"-1.816513\",\"52.5487566\"],[\"-1.8164913\",\"52.548824\"]]";
    Type listType = new TypeToken<List<List<Double>>>() {}.getType();
    List<List<Double>> numbers = (List<List<Double>>) gson.fromJson(jsonOutput, listType);
    for(List<Double> inner: numbers){
      for(Double d: inner){
        System.out.println(d);
      }
    }

  }
}

在上下文中并不重要,但供将来参考:Java 7、Gson 2.2.4

【讨论】:

  • 类型解析,不错 => +1
  • 谢谢 Trapo。在使用 String 而不是 Double 类型后它起作用了。
  • 在我看来它们是双倍的;)。
  • :) 当然他们应该有,但是在这种形式下它给出了这个错误:预期 BEGIN_ARRAY 但在第 1 行第 3 列是字符串
  • 只是因为我很好奇。你用的是和我一样的Gson版本吗?此外,你确定你没有多余的“?
【解决方案2】:

一种解决方案,带有原始类型:

package com.stackoverflow.so18525283;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.List;

public class App {

    private static final String INPUT = "[[\"-1.816513\",\"52.5487566\"],[\"-1.8164913\",\"52.548824\"]]";

    public static void main(final String[] args) {
        final Gson gson = new GsonBuilder().create();
        final List<?> fromJson = gson.fromJson(INPUT, List.class);
        if (fromJson != null && fromJson.size() > 0 && fromJson.get(0) instanceof List) {
            System.out.println(((List<?>) fromJson.get(0)).get(0)); // this is a String
        }
    }
}

另一种解决方案是重新创建一个有效的 JSON 对象,与下面的 App 相同,但具有:

public static void main(String[] args) {
    final Gson gson = new GsonBuilder().create();
    final Foo fromJson = gson.fromJson("{ data: " + INPUT + "}", Foo.class);
    // TODO: check for null
    System.out.println(fromJson.data.get(0).get(0)); // this is a Double
}

private static class Foo {
    List<List<Double>> data;
}

【讨论】:

  • "包 com.stackoverflow.so18525283;"好的!我冒昧地复制你的包命名风格:)
猜你喜欢
  • 1970-01-01
  • 2020-01-30
  • 1970-01-01
  • 1970-01-01
  • 2018-08-12
  • 1970-01-01
  • 2020-04-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多