无法从Json解析 [英] Can't Resolve fromJson
问题描述
我查看了此主题和这个线程都试图让android studio识别 fromJson
方法没有运气。我的项目结构中有 com.google.code.gson:gson:2.6.2
作为我的 gson
依赖项。所以,我试过的所有东西都不会允许这一行编译:
我尝试了以下导入,但没有运气,尽我最大努力每次清理项目用于某种刷新。导入:
import java.lang.reflect.Type;
import com.google.gson.JsonObject;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.stream.JsonReader;
import com.google.gson.reflect.TypeToken;
我应该尝试别的吗?我在 google的文档。
这是gson变量的一些上下文,因为我意识到这是深夜,当我命名这些东西时。在 doInBackground()
方法中,我声明了一个变量 gSB
就像这样:
StringBuilder gSB = new StringBuilder();
里面有一个 try
所以:
connectStat =(HttpURLConnection)params [0] .openConnection();
InputStream inputer = new BufferedInputStream(connectStat.getInputStream());
scanner =新的扫描仪(inputer);
while(scanner.hasNext())gSB.append(scanner.nextLine());
Log.v(checkRSP,Response(+ connectStat.getResponseCode()+):+
connectStat.getResponseMessage());
所有异常处理正确,然后在 try
,捕获
,最后
。变量 gson
分配如下:
字符串gson = gSB。的toString();
然后,
LookupRate cuRate = gson.fromJson( gson,LookupRate.class);
第一个参数 gson
无效
其中一个重载 - 我认为你正在尝试使用 - 需要 String
,其中包含JSON内容。
所以你的代码应该是:
//假设您的JSON内容存储在变量名称myJsonStr中
LookupRate cuRate = gson.fromJson(myJsonStr,LookupRate.class);
您只需填写 myJsonStr
表格你得到你的数据(网络服务,数据库,本地文件等等)
I've looked over this thread and this thread all trying to get android studio to recognize the fromJson
method with no luck. I have com.google.code.gson:gson:2.6.2
as my gson
dependency in my project structure. So, far all I've tried will still not allow this line to compile:
LookupRate cuRate = gson.fromJson(gson, LookupRate.class);
I've tried the following imports with no luck making my best effort to clean my project each time just for some kind of refresh. Imports:
import java.lang.reflect.Type;
import com.google.gson.JsonObject;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.stream.JsonReader;
import com.google.gson.reflect.TypeToken;
Should I be trying something else? I look over at what each of the two parameters should be dependent upon in google's documentation.
Here is some context for the gson variable as I realized it was late at night when I named some of this stuff. In the doInBackground()
method I declared a variable gSB
like so:
StringBuilder gSB = new StringBuilder();
inside of a try
statement it assigned like so:
connectStat = (HttpURLConnection) params[0].openConnection();
InputStream inputer = new BufferedInputStream(connectStat.getInputStream());
scanner = new Scanner(inputer);
while (scanner.hasNext()) gSB.append(scanner.nextLine());
Log.v(checkRSP, "Response(" + connectStat.getResponseCode() + "):" +
connectStat.getResponseMessage());
All exceptions are handled properly then after my try
,catch
,finally
. The variable gson
is assigned like so:
String gson = gSB.toString();
Then the line where fromJson
cannot be resolved comes next, but I will repeat it to remove any confusion my amateur description may have caused:
LookupRate cuRate = gson.fromJson(gson, LookupRate.class);
the first parameter gson
is not valid
one of the overloads -which i think you are trying to use- takes a String
that contains the JSON content.
so your code should be:
//assume your JSON content is stored in variable name `myJsonStr`
LookupRate cuRate = gson.fromJson(myJsonStr, LookupRate.class);
you just need to fill myJsonStr
form wherever you get your data (web-service, database,local file,...etc)
这篇关于无法从Json解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!