Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从JSON字符串中提取数据_Java_Android_Json - Fatal编程技术网

Java 从JSON字符串中提取数据

Java 从JSON字符串中提取数据,java,android,json,Java,Android,Json,我试图从中提取数据,但得到的是一个空白屏幕。这是我的JSON解析代码 try { JSONObject bookObject = new JSONObject(SAMPLE); JSONArray booksArray = bookObject.getJSONArray("books"); for (int i = 0; i < booksArray.length(); i++){ JSONObject curre

我试图从中提取数据,但得到的是一个空白屏幕。这是我的JSON解析代码

try {

        JSONObject bookObject = new JSONObject(SAMPLE);
        JSONArray booksArray = bookObject.getJSONArray("books");

        for (int i = 0; i < booksArray.length(); i++){

           JSONObject currentBook = booksArray.getJSONObject(i);

           String title =  currentBook.getString("title");
           String author = currentBook.getString("author");
           String isbn = currentBook.getString("isbn");

           Book book = new Book(title,author,isbn);
           books.add(book);
        }

}catch (JSONException e){
        Log.e("QueryUtils", "Problem parsing the earthquake JSON results", e);
}
试试看{
JSONObject bookObject=新JSONObject(示例);
JSONArray booksArray=bookObject.getJSONArray(“books”);
对于(int i=0;i
我建议您使用GSON,这是一个非常简单的库

到Json

Gson gson = new Gson();

Staff obj = new Staff();

// 1. Java object to JSON file
gson.toJson(obj, new FileWriter("C:\\projects\\staff.json"));

// 2. Java object to JSON string
String jsonInString = gson.toJson(obj);
Gson gson = new Gson();

// 1. JSON file to Java object
Staff staff = gson.fromJson(new FileReader("C:\\projects\\staff.json"), Staff.class);

// 2. JSON string to Java object
String json = "{'name' : 'mkyong'}";
Staff staff = gson.fromJson(json, Staff.class);

// 3. JSON file to JsonElement, later String
JsonElement json = gson.fromJson(new FileReader("C:\\projects\\staff.json"), JsonElement.class);
String result = gson.toJson(json);
来自Json

Gson gson = new Gson();

Staff obj = new Staff();

// 1. Java object to JSON file
gson.toJson(obj, new FileWriter("C:\\projects\\staff.json"));

// 2. Java object to JSON string
String jsonInString = gson.toJson(obj);
Gson gson = new Gson();

// 1. JSON file to Java object
Staff staff = gson.fromJson(new FileReader("C:\\projects\\staff.json"), Staff.class);

// 2. JSON string to Java object
String json = "{'name' : 'mkyong'}";
Staff staff = gson.fromJson(json, Staff.class);

// 3. JSON file to JsonElement, later String
JsonElement json = gson.fromJson(new FileReader("C:\\projects\\staff.json"), JsonElement.class);
String result = gson.toJson(json);

如果您想查看有关此的更多信息,可以查看此链接:

检查您的模型类,在其中设置参数。

尝试以下操作:

    Gson gson = new Gson();

    JSONObject o = new JSONObject(jsonData.toString()); // pass your data here
    JSONArray arr = new JSONArray(o.get("books").toString());

    List<Book> books = new ArrayList<Book>();

    for (int i = 0; i < arr.length(); i++) {
        Book book = gson.fromJson(arr.get(i).toString(), Book.class);
        books.add(book);
    }
Gson-Gson=new-Gson();
JSONObject o=新的JSONObject(jsonData.toString());//在这里传递您的数据
JSONArray arr=新的JSONArray(o.get(“books”).toString();
List books=new ArrayList();
对于(int i=0;i
我用过这里的格森图书馆。 还有其他的图书馆。
有关更多详细信息,请参阅此链接:

您需要将网络服务的响应转换为字符串,然后获取它将工作的jsonArray

像这样:

@Override
public void onResponse(Call call, final okhttp3.Response response) throws IOException {
                    final String stringResponse = response.body().string();
                    //insted of sample pass the stringresponse it will work
                    JSONObject bookObject = new JSONObject(stringResponse);
                    JSONArray booksArray = bookObject.getJSONArray("books");

            for (int i = 0; i < booksArray.length(); i++){

                JSONObject currentBook = booksArray.getJSONObject(i);

               String title =  currentBook.getString("title");
               String author = currentBook.getString("author");
               String isbn = currentBook.getString("isbn");

                Book book = new Book(title,author,isbn);
                books.add(book);
            }
            });
@覆盖
public void onResponse(调用,最终okhttp3.Response)抛出IOException{
最终字符串stringResponse=response.body().String();
//代替了通过stringresponse的示例,它将起作用
JSONObject bookObject=新的JSONObject(stringResponse);
JSONArray booksArray=bookObject.getJSONArray(“books”);
对于(int i=0;i
示例
包含类似于链接的json字符串?你想要实现什么,你所说的“我所得到的是一个空白屏幕”是什么意思?考虑使用GSON,创建一个对应JSON的java对象,然后简单地将JSON字符串转换为对象。