我正在通过PHP在服务器上创建JSON(如果我正在创建错误的JSON,请告诉我如何才能使其正确)。现在我想将此字符串转换为Android上的JSON对象。
{
"account[0]":{"0":"http:\/\/twitter.com\/OGTedBerg"},
"image[0]":{"0":"http:\/\/a0.twimg.com\/profile_images\/2821392281\/59f4fe1cbb44c8812401192a199b4b04_normal.jpeg"},
"name[0]":"Ted Berg",
"tweet[0]":"I know the flavor I most closely associate with the <em>United<\/em> <em>States<\/em> is Cheesy Paprika. <a target=\"_blank\" href=\"http:\/\/t.co\/5On9CQuV\">http:\/\/t.co\/5On9CQuV<\/a>",
"time[0]":"1 minute ago"
}
{
"account[1]":{"0":"http:\/\/twitter.com\/sirgarrick45"},
"image[1]":{"0":"http:\/\/a0.twimg.com\/profile_images\/3071585947\/207a70fcc09749a1359287a204db1c0b_normal.jpeg"},
"name[1]":"Mike Green",
"tweet[1]":"RT @<a class=\" \" href=\"https:\/\/twitter.com\/RussellVogt\">RussellVogt<\/a>: <a target=\"_blank\" href=\"http:\/\/search.twitter.com\/search?q=%23NDAA\" title=\"#NDAA\" class=\" \">#NDAA<\/a> <em>United<\/em> <em>States<\/em> of America Check it out!! <a target=\"_blank\" href=\"http:\/\/t.co\/KxRx3fMH\">http:\/\/t.co\/KxRx3fMH<\/a>",
"time[1]":"1 minute ago"
}
{
"account[2]":{"0":"http:\/\/twitter.com\/Gabriel_Enrike"},
"image[2]":{"0":"http:\/\/a0.twimg.com\/profile_images\/2921364087\/282fec08788f1d96eb0d1167a2639a69_normal.jpeg"},
"name[2]":"GabrielEnrique",
"tweet[2]":"@<a class=\" \" href=\"https:\/\/twitter.com\/BarackObama\">BarackObama<\/a> will oath to his second term as president from the <em>United<\/em> <em>States<\/em> on Martin Lutker King jr. day! <a target=\"_blank\" href=\"http:\/\/search.twitter.com\/search?q=%23HeHadADream\" title=\"#HeHadADream\" class=\" \">#HeHadADream<\/a> <a target=\"_blank\" href=\"http:\/\/search.twitter.com\/search?q=%23TeamBO\" title=\"#TeamBO\" class=\" \">#TeamBO<\/a> <a target=\"_blank\" href=\"http:\/\/search.twitter.com\/search?q=%23YesWeCan\" title=\"#YesWeCan\" class=\" \">#YesWeCan<\/a>",
"time[2]":"1 minute ago"
}
{
"account[3]":{"0":"http:\/\/twitter.com\/Davis_T94"},
"image[3]":{"0":"http:\/\/a0.twimg.com\/profile_images\/2627985413\/image_normal.jpg"},
"name[3]":"tim davis",
"tweet[3]":"RT @<a class=\" \" href=\"https:\/\/twitter.com\/FactBoook\">FactBoook<\/a>: The <em>United<\/em> <em>States<\/em> once captured Mexico City.",
"time[3]":"1 minute ago"
}
这是我用来将字符串转换为JSON对象的Java代码。
private String[] acnt;
private String[] img;
private String[] nam;
private String[] twt;
private String[] tim;
JSONObject obj = null;
// creating response string to jsonobject and getting required values
try {
obj = new JSONObject(twitter_response);
int lenght = obj.length();
acnt = new String[lenght];
img = new String[lenght];
nam = new String[lenght];
twt = new String[lenght];
tim = new String[lenght];
//obj.getJSONArray(twitter_response);
for ( $i = 0; $i < lenght; $i++ )
{
acnt[$i] = new String(obj.getString("account[" + $i + "]"));
img[$i] = new String(obj.getString("image[" + $i + "]"));
nam[$i] = new String(obj.getString("name[" + $i + "]"));
twt[$i] = new String(obj.getString("tweet[" + $i + "]"));
tim[$i] = new String(obj.getString("time[" + $i + "]"));
}
} catch (JSONException e) {
Log.e("log_tag", "Error receiving tweet "+ e.toString());
e.printStackTrace();
}
有了这个我只得到第一个价值;知道我做错了什么吗?
答案 0 :(得分:0)
您的输入无效JSON。 JSON数组使用模式[ { element }, { element }, { element } ...]
,而不是您正在使用的括号语法。
Here's a related question关于如何使用JSON格式化数组,以及here's some precise documentation一般关于JSON语法。
编辑:我会说你发布的JSON样本非常接近有效(实际上,每个单独的块本身都是有效的,如果你试图解析整个集合,它就无效 - 是)。也就是说,我仍然认为在更传统的JSON数组结构中格式化数据可能更有意义,例如:
[
{
"account":{"0":"http:\/\/twitter.com\/OGTedBerg"},
"image":{"0":"http:\/\/a0.twimg.com\/profile_images\/2821392281\/59f4fe1cbb44c8812401192a199b4b04_normal.jpeg"},
"name":"Ted Berg",
"tweet":"I know the flavor I most closely associate with the <em>United<\/em> <em>States<\/em> is Cheesy Paprika. <a target=\"_blank\" href=\"http:\/\/t.co\/5On9CQuV\">http:\/\/t.co\/5On9CQuV<\/a>",
"time":"1 minute ago"
},
...
]
这可能会使解析过程变得更加简单。