0

我正在通过这样的 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();
        }

有了这个,我只得到第一个值;知道我做错了什么吗?

4

1 回答 1

0

您的输入不是有效的 JSON。JSON 数组使用 pattern [ { element }, { element }, { element } ...],而不是您正在使用的括号语法。

这是一个关于如何在 JSON 中格式化数组的相关问题,这里是关于 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"
  },
  ...
]

这可能会使解析过程变得相当简单。

于 2013-01-09T17:19:39.493 回答