java 在android中反序列化JSON字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38135957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Deserializing JSON string in android
提问by JamesYTL
I have JSON String that read as below
我有如下所示的 JSON 字符串
{ "Status":"Clear", "Class": [{ <br>
{"name":"personA", "available" : 1}, <br>
{"name":"personB", "available" : 0}, <br>
{"name":"personC", "available" : 0}, <br>
{"name":"personD", "available" : 1} <br>
}] }
How do I deserialize the JSON String above?
如何反序列化上面的 JSON 字符串?
I got the idea of using JSONObject to take in whole JSON String into it but no idea how to get the arraylist of object (the list of Class object) and assign to a arraylist<Class>.
我想到了使用 JSONObject 将整个 JSON 字符串纳入其中,但不知道如何获取对象的数组列表(类对象的列表)并分配给一个数组列表<Class>。
Guidance and help are much appreciated.
非常感谢指导和帮助。
UPDATED:
更新:
SOLVED AND SOLUTION SHOWN
已解决并显示解决方案
I have solved this question but I think I have to explain how and why I used such solution.
我已经解决了这个问题,但我想我必须解释我如何以及为什么使用这样的解决方案。
So to further explain my question, this is an json string that originally an object that got serialized and sent from a webservice to my apps.
所以为了进一步解释我的问题,这是一个 json 字符串,它最初是一个被序列化并从网络服务发送到我的应用程序的对象。
the original object is like this :
原始对象是这样的:
public class StatusJson
{
public String Status { get; set; }
public List<Class> Class { get; set; }
}
So what I have to do is just declare an exactly same class in my android then use this code
所以我要做的就是在我的 android 中声明一个完全相同的类然后使用这个代码
statusJson statusJSON=g.fromJson(JSonString,StatusJson.class);
which will automatically parse the json string to the exact same class format.
它将自动将 json 字符串解析为完全相同的类格式。
Hope this will help you guys too if you are directly sending a JSON serialized class like me.
如果您像我一样直接发送 JSON 序列化类,希望这对你们也有帮助。
采纳答案by thealeksandr
I suggest you to check Gson library.
我建议你检查Gson 库。
You can create a class with anotations
您可以创建一个带有注释的类
private class ClassObj {
@SerializedName("personA")
private final String personA;
....
}
And then
接着
ClassObj object = gson.fromJson(jsonString, ClassObj.class);
It can be complicated object, which contain other gson objects or Collection. Try.
它可以是复杂的对象,其中包含其他 gson 对象或 Collection。尝试。
回答by Yasir Abbas
I believe this is an invalid Json. Your class attribute is an array and an object.
我相信这是一个无效的 Json。您的类属性是一个数组和一个对象。
回答by Alias
You need to use GSON library for example:
例如,您需要使用 GSON 库:
@SerializedName("studentName") public String name;
@SerializedName("studentName") 公共字符串名称;
回答by AfamO
There is a need to add a somewhat missing feature in the above first(thealeksandr)'s answer:
需要在上面的 first(thealeksandr) 的回答中添加一个有点缺失的功能:
Gson gson = new GsonBuilder().create();
You can now use this instantiated object gson
in the above ClassObj object = gson.fromJson(jsonString, ClassObj.class);
To do this simply use GsonBuilder
class in the Gson Libraryas I just demonstrated.
你现在可以gson
在上面使用这个实例化的对象ClassObj object = gson.fromJson(jsonString, ClassObj.class);
要做到这一点,只需使用我刚刚演示GsonBuilder
的Gson 库中的类。
回答by Submersed
Look at the API's of JSONObjectand JSONArray.
查看JSONObject和JSONArray的 API 。
You should be able to figure it out from there. You just create a JSONObject out of the string:
你应该能够从那里弄清楚。您只需从字符串中创建一个 JSONObject:
ex. JSONObject jsonAsObj = new JSONObject(my_json_string)
;
前任。JSONObject jsonAsObj = new JSONObject(my_json_string)
;
Then use JSONArray classArray = jsonAsObject.getJSONArray("Class");
to get your array...
然后用于JSONArray classArray = jsonAsObject.getJSONArray("Class");
获取您的阵列...
As far as converting it to an ArrayList, you need to create your type and traverse the JSONArray and create objects from it, and add them to your ArrayList. Or you could look into another helper library, like GSON.
至于将其转换为 ArrayList,您需要创建您的类型并遍历 JSONArray 并从中创建对象,并将它们添加到您的 ArrayList。或者您可以查看另一个帮助程序库,例如GSON。