2013-08-30 19 views
3

我想解析一個字符串與Gson庫,但沒有成功。這是我的字符串:Gson解析字符串,沒有鍵值對

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

這個例子中的問題是沒有鍵 - 值對。我看了其他例子,但他們都有鍵值對,看起來不像我的問題。

+0

http://stackoverflow.com/questions/2034643/parsing-json-feeds-with-google-gson?rq=1 – 2013-08-30 05:04:04

+1

的可能的複製我更新了關於發佈的問題 – ismail

+0

我認爲[這可能是相關的](http://stackoverflow.com/questions/10800508/)。但我同意OP,這不是一個候選人關閉重複,至少不是原來的鏈接。這是一個非常不同的解決方案的單獨問題。 – Brian

回答

3

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

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

鍵入解析,nice => +1 – 2013-08-30 05:53:24

+0

感謝trapo。在使用String而不是Double類型之後它就起作用了。 – ismail

+0

他們似乎是我的雙打;)。 – giampaolo

1

的一個解決方案,與原始類型:

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; 
} 
+0

」package com.stackoverflow.so18525283;「nice!我冒昧地複製你的包命名風格:) – giampaolo