Java Android:处理可以是字符串或数组的json对象

Java Android:处理可以是字符串或数组的json对象,java,android,arrays,json,Java,Android,Arrays,Json,遇到了不确定如何处理的情况 我有来自服务器的json数据;例如:(我只是发布了json的一部分,所以,是的,json是有效的) 创建了一个类来存储此json数据 public class feedFormat{ Integer wall_id; String poster_image_thumbnail; String post_type; String post_content; } 有时,post_内容可以是空的,也可以是上面示例中的数组。我已经以feedF

遇到了不确定如何处理的情况

我有来自服务器的json数据;例如:(我只是发布了json的一部分,所以,是的,json是有效的)

创建了一个类来存储此json数据

public class feedFormat{
    Integer wall_id;
    String poster_image_thumbnail;
    String post_type;
    String post_content;
}
有时,post_内容可以是空的,也可以是上面示例中的数组。我已经以feedFormat将post_内容声明为字符串。这显然是在抛出一个强制转换异常(将数组转换为字符串?)

我本来希望JSONObject将其作为字符串读取,然后再从那里将其转换为数组,但似乎没有这样做

如何动态处理字符串或数组?如果它是一个数组,我需要分解它

我正在将这个应用程序从IOS移植到android,IOS中有一个“id”对象,可以是任何类别。我检查类是NSSTring还是NSArray,然后从那里获取它。在Java中,我不确定如何处理它


非常感谢您的建议

如果您的JSON数组为空,则如下所示:

"post_content": []
然后它将仍然是一个数组,具有0大小的特殊性

然后,我建议您直接将JSON数组解析为适当的数据结构,不管大小如何,例如ArrayList>。然后,您将能够遍历JSON数组的所有项,并为每个项在arraylist中添加一个新的HashMap。每个hashmap将包含三对键值

但是,如果我很理解您的JSON,它似乎总是一个由三个元素组成的数组,第三个元素本身就是一个数组,其大小由属性images\u count决定。这不是很好,您的JSON结构应该是:

"post_content": {
    "text": "",
    "images": [
        "https://fbcdn-sphotos-a-a.akamaihd.net/hphotos-ak-ash4/227408_475848819113499_663318592_n.jpg"
    ]
}

由于图像是一个数组,您可以很容易地获得它的大小。

JSONObject
具有名为
has(String key)
的函数,用于检查是否存在键的映射,
isNull(String key)
用于检查特定键是否为
null
。阅读前使用这些检查键。

您可以这样检查
jsonobject.has(“post\u内容”)

这是做你想做的事情最简单的方法。另一种方法是创建另一个类

public class FeedFormat{
    Integer wall_id;
    String poster_image_thumbnail;
    String post_type;
    ArrayList<PostContent> post_content = new ArrayList<PostContent>();
}

public class PostContent {
    String text;
    Integer imageCount;
    ArrayList<String> images = new ArrayList<String>();
}
公共类FeedFormat{
整数墙标识;
字符串海报\图像\缩略图;
字符串后置类型;
ArrayList post_content=新建ArrayList();
}
公共类后内容{
字符串文本;
整数图像计数;
ArrayList images=新的ArrayList();
}

这样,您就可以将每个帖子内容处理到特定的对象中,而不必使用JSONObject/JSONArray。

您可以使用如下内容:

String data= "wall_id": 889149,
"poster_image_thumbnail": "http:\/\/www.mface.me\/images\/avatar\/thumb_62441559ddb1dda7513d0f94.jpg",
"post_type": "profile",
"post_content": [{
    "text": "",
    "images_count": 1,
    "images": ["https:\/\/fbcdn-sphotos-a-a.akamaihd.net\/hphotos-ak-ash4\/227408_475848819113499_663318592_n.jpg"]
}]

JSONArray jArray=data.getJSONArray("post_content");
for(int i=0; i<jArray.length(); i++)
{
JSONObject jObj=jArray.getJSONObject(i);
String text=jObj.getString("text");
int images_count=jObj.getInt("images_count");
String images=jObj.getInt("images");
}
String data=“wall_id”:889149,
“海报图片缩略图”:“http:\/\/www.mface.me\/images\/avatar\/thumb\u 62441559ddb1dda7513d0f94.jpg”,
“post_type”:“profile”,
“发布内容”:[{
“文本”:“,
“图像计数”:1,
“图像”:[“https:\/\/fbcdn-sphotos-a-a.akamaihd.net\/hphotos-ak-ash4\/227408_475848819113499_663318592_n.jpg”]
}]
JSONArray jArray=data.getJSONArray(“post_内容”);

对于(int i=0;iI)我说使用数组,如果是空字符串,则改为使用空数组。如果您的post_内容提供的是空数组而不是空数组,请使用@raphael titol的答案。谢谢raphael,但此时我无法更改post_内容的方式,它仍然是“”-我无法更改它,因为我的iphone应用程序已经在使用它。我知道您无法更改它。在这种情况下,我建议您在Android应用程序中,首先检查postContent是否为JSONArray。如果是,请按照我指示的方式解析它:使用arrayList。但是,如果您确定postContent包含的元素不超过1个,则可以直接将“文本”存储在字符串中,并将“images\u count”强制转换为整数,然后对“images”数组的每个元素使用ArrayList。希望这能有所帮助!抱歉我的无知,我在这里有点迷路。我尝试检查post\u内容是否是带有if(currentRecord.get(“post\u content”)instanceof JSONArray)的数组-当post_内容为数组时,这显示为真,但我似乎无法分解数组,现在编辑我的问题您的数组确实只包含一个对象。如您所见,分隔数组的[]包含一对或{},它对您的对象进行了定界。但是,您的对象包含3个元素。使用大小始终为1的数组不是正确的方法,这就是为什么我前面提到,您的JSON应该调整为JSON对象,包含一个字符串和一个JSON数组元素。为了更好地理解JSON语法,我建议您查看is link:嘿,拉斐尔,是的,我几分钟前就知道了。那纯粹是我的错误,也谢谢你的回答。我想我只是需要休息一下我的大脑;我一直在进行一场编码马拉松,我错过了一些简单的错误。谢谢西里尔,如果内容是空的,这会起作用吗?我会尝试一下,看看。如果内容是空的,它会起作用,但是在使用数据之前(例如,您希望检索所有post_内容的文本),请检查
post_内容
是否为空:
.lenght()
methode for JSONArray或ArrayList这样做。当post_内容为空时,这不会引发类强制转换异常吗?
public class FeedFormat{
    Integer wall_id;
    String poster_image_thumbnail;
    String post_type;
    JSONArray post_content;
}

feedFormat toto = new feedFormat();
toto.post_content = yourJsonObject.getJsonArray("post_content");
public class FeedFormat{
    Integer wall_id;
    String poster_image_thumbnail;
    String post_type;
    ArrayList<PostContent> post_content = new ArrayList<PostContent>();
}

public class PostContent {
    String text;
    Integer imageCount;
    ArrayList<String> images = new ArrayList<String>();
}
String data= "wall_id": 889149,
"poster_image_thumbnail": "http:\/\/www.mface.me\/images\/avatar\/thumb_62441559ddb1dda7513d0f94.jpg",
"post_type": "profile",
"post_content": [{
    "text": "",
    "images_count": 1,
    "images": ["https:\/\/fbcdn-sphotos-a-a.akamaihd.net\/hphotos-ak-ash4\/227408_475848819113499_663318592_n.jpg"]
}]

JSONArray jArray=data.getJSONArray("post_content");
for(int i=0; i<jArray.length(); i++)
{
JSONObject jObj=jArray.getJSONObject(i);
String text=jObj.getString("text");
int images_count=jObj.getInt("images_count");
String images=jObj.getInt("images");
}