简体   繁体   English

在Android中反序列化JSON字符串

[英]Deserializing JSON string in android

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字符串放入其中的想法,但不知道如何获取对象的arraylist(Class对象的列表)并分配给arraylist <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字符串,最初是一个对象,该对象已被序列化并从Web服务发送到我的应用程序。

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序列化类,希望对您也有帮助。

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. 它可以是复杂的对象,其中包含其他gson对象或Collection。 Try. 尝试。

I believe this is an invalid Json. 我相信这是无效的杰森。 Your class attribute is an array and an object. 您的class属性是一个数组和一个对象。

Look at the API's of JSONObject and JSONArray . 查看JSONObjectJSONArray的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"); 然后使用JSONArray classArray = jsonAsObject.getJSONArray("Class"); to get your array... 得到你的数组...

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. 至于将其转换为ArrayList,您需要创建类型并遍历JSONArray并从中创建对象,然后将它们添加到ArrayList中。 Or you could look into another helper library, like GSON . 或者,您可以查看另一个帮助程序库,例如GSON

You need to use GSON library for example: 您需要使用GSON库,例如:

@SerializedName("studentName") public String name; @SerializedName(“ studentName”)公共字符串名称;

There is a need to add a somewhat missing feature in the above first(thealeksandr)'s answer: 有必要在上述第一(thealeksandr)的答案中添加一些缺少的功能:

 Gson gson = new GsonBuilder().create();

You can now use this instantiated object gson in the above ClassObj object = gson.fromJson(jsonString, ClassObj.class); 现在,您可以在上述ClassObj object = gson.fromJson(jsonString, ClassObj.class);使用此实例化对象gson ClassObj object = gson.fromJson(jsonString, ClassObj.class); To do this simply use GsonBuilder class in the Gson Library as I just demonstrated. 为此,只需使用我刚刚演示的Gson库中的GsonBuilder类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM