android - 将 JSON 响应转换为 JAVA 对象

标签 android json gson

我正在尝试读取 JSON 文件以将其转换为 Java 对象,但出现错误。此 JSON 响应代码来自 API。这是我的主要 Activity 。这是我第一次提问,所以请帮助我。

package com.example.ahsan.jsontogson;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import java.io.FileReader;
import java.util.List;
public class MainActivity extends AppCompatActivity {
public static final String TAG = MainActivity.class.getSimpleName();
public static final String mPath = "Json.txt";
private QuoteBank mQuoteBank;
private List<String> mLines;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    Gson gson = new Gson();
    // 1. i am getting error on this line source file is not located error
    QuoteBank staff = gson.fromJson(new FileReader("D:\\JSON.txt"),    QuoteBank.class);
    // 2. JSON to Java object, read it from a Json String.
    String jsonInString = "{'name' : 'mkyong'}";
    Staff staff = gson.fromJson(jsonInString, Staff.class);
    // JSON to JsonElement, convert to String later.
    JsonElement json = gson.fromJson(new FileReader("D:\\file.Json"), JsonElement.class);
    String result = gson.toJson(json);
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

最佳答案

原因是您正试图从台式机/笔记本电脑上的设备读取该文件。

 QuoteBank staff = gson.fromJson(new FileReader("D:\\JSON.txt"),    QuoteBank.class); //D drive path

你不能以任何方式这样做。 该文件应该存在于您的设备外部存储目录中的某个位置。

关于android - 将 JSON 响应转换为 JAVA 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37621170/

相关文章:

java - Facebook LikeView 更改按钮大小

java - 如何使用 java 在 Web 应用程序中公开 JSON

json - 如果键匹配,则从 bash 中的 json 中提取特定键值

kotlin - 使用 Klaxon 使用动态键解析 map

java - Gson HTTP 响应对象

java - 管理java gson后,json文件中的unicode字符不会被转换

android - 尝试使用 Android Drawing 绘制图形时出现 NullPointerException

java - 使用 Retrofit2 将文件上传到 slack

php - 无法在Android Studio中使用PHP从MySQL数据库获取数据

javascript - 将国家/地区 json 与 selectize 插件一起使用