我有一个JSON的Array数组,如下所示
{
"width" : 500,
"numbers" : [
[46, 11, "1674"],
[46, 11, "1673"],
[46, 11, "1677"],
[46, 11, "1678"],
[46, 11, "1674"],
[46, 11, 1673]
]
}我不知道如何解析它。
JSONObject json = new JSONObject(jsonString);
JSONArray jsonArray = json.getJSONArray("numbers");此代码抛出类型不匹配错误。
发布于 2011-09-08 18:06:07
尝试GSON,下面的代码应该可以解决这个问题:
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import com.google.gson.Gson;
public class ReadTest {
public static void main(String[] args) throws IOException {
String json = FileUtils.readFileToString(new File("json.txt"));
Gson gson = new Gson();
A a = gson.fromJson(json, A.class);
System.out.println(a.width);
System.out.println(a.numbers[0][0]);
}
}
public class A {
public int width;
public int numbers[][];
}https://stackoverflow.com/questions/7345359
复制相似问题