用Java读取JSON二维数组
每个用Java读取JSON二维数组,java,android,json,Java,Android,Json,每个新闻条目都有三项内容:标题、内容和日期 这些条目是从数据库中检索的,我希望使用JSONObject和JSONArray在应用程序中读取它们。但是,我不知道如何使用这些类 这是我的JSON字符串: [ { "news":{ "title":"5th title", "content":"5th content", "date":"1363197493" } }, { "news":{
新闻
条目都有三项内容:标题
、内容
和日期
这些条目是从数据库中检索的,我希望使用JSONObject和JSONArray在应用程序中读取它们。但是,我不知道如何使用这些类
这是我的JSON字符串:
[
{
"news":{
"title":"5th title",
"content":"5th content",
"date":"1363197493"
}
},
{
"news":{
"title":"4th title",
"content":"4th content",
"date":"1363197454"
}
},
{
"news":{
"title":"3rd title",
"content":"3rd content",
"date":"1363197443"
}
},
{
"news":{
"title":"2nd title",
"content":"2nd content",
"date":"1363197409"
}
},
{
"news":{
"title":"1st title",
"content":"1st content",
"date":"1363197399"
}
}
]
JSON字符串是一个
JSONObject
的JSONArray
,其中包含一个名为“news”的内部JSONObject
尝试以下方法对其进行分析:
JSONArray array = new JSONArray(jsonString);
for(int i = 0; i < array.length(); i++) {
JSONObject obj = array.getJSONObject(i);
JSONObject innerObject = obj.getJSONObject("news");
String title = innerObject.getString("title");
String content = innerObject.getString("content");
String date = innerObject.getString("date");
/* Use your title, content, and date variables here */
}
JSONArray数组=新的JSONArray(jsonString);
对于(int i=0;i
首先,您的JSON结构并不理想。您有一个对象数组,每个对象中都有一个对象。但是,您可以这样读:
JSONArray jsonArray = new JSONArray (jsonString);
int arrayLength = jsonArray.length ();
for (int counter = 0; counter < arrayLength; counter ++) {
JSONObject thisJson = jsonArray.getJSONObject (counter);
// we finally get to the proper object
thisJson = thisJson.getJSONObject ("news");
String title = thisJson.getString ("title");
String content = thisJson.getString ("content");
String date = thisJson.getString ("date");
}
然后,您可以按如下方式解析它:
JSONArray jsonArray = new JSONArray (jsonString);
int arrayLength = jsonArray.length ();
for (int counter = 0; counter < arrayLength; counter ++) {
// we don't need to look for a named object any more
JSONObject thisJson = jsonArray.getJSONObject (counter);
String title = thisJson.getString ("title");
String content = thisJson.getString ("content");
String date = thisJson.getString ("date");
}
JSONArray-JSONArray=新的JSONArray(jsonString);
int-arrayLength=jsonArray.length();
用于(int计数器=0;计数器<阵列长度;计数器++){
//我们不再需要寻找命名对象
JSONObject thisJson=jsonArray.getJSONObject(计数器);
String title=thisJson.getString(“title”);
String content=thisJson.getString(“内容”);
字符串日期=thisJson.getString(“日期”);
}
首先,创建一个名为News的类,包含3个字段:title、content和date。阅读有关如何使用java json解析器的文档。如果日期是一个字符串,则使用getLong()无法获取该字符串,请检查引号“Your json is bad”是否正确。数组可能包含除“news”之外的其他对象,因此json的格式实际上是非常具有描述性的。@Go他实际上在标题中说这是一个二维数组,在问题的第一行他说“每个新闻有三个东西:标题、内容和日期”。我认为他并不真正理解JSON数组和对象的语法,所以我试着用一个例子来帮助他。我认为Shade是对的,我的JSON没有他建议的那么好,只是因为我用php编码,这就是我得到的,我不能像你那样做,但我会尝试。无论如何,谢谢你的帮助!:)没问题。您可以对生成JSON的PHP代码提出另一个问题。@JonathanHugh我很高兴您能找到问题的答案。请记住,如果给出的答案满足您的需要,您应该通过单击投票箭头下的复选标记来接受该答案,以便将来查看此问题的人能够知道哪个答案解决了问题。
JSONArray jsonArray = new JSONArray (jsonString);
int arrayLength = jsonArray.length ();
for (int counter = 0; counter < arrayLength; counter ++) {
// we don't need to look for a named object any more
JSONObject thisJson = jsonArray.getJSONObject (counter);
String title = thisJson.getString ("title");
String content = thisJson.getString ("content");
String date = thisJson.getString ("date");
}