Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中将字符串转换为JSON对象_Android_Json - Fatal编程技术网

如何在Android中将字符串转换为JSON对象

如何在Android中将字符串转换为JSON对象,android,json,Android,Json,我是这样通过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"}, "

我是这样通过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"
  }
{
“帐户[0]”:{“0”:“http:\/\/twitter.com\/OGTedBerg”},
“image[0]:{“0”:“http:\/\/a0.twimg.com\/profile\u images\/2821392281\/59f4fe1cbb44c8812401192a199b4b04\u normal.jpeg”,
“名称[0]:“Ted Berg”,
“tweet[0]:“我知道我最喜欢的美国风味是奶酪辣椒。http:\/\/t.co\/5On9CQuV”,
“时间[0]:“1分钟前”
}
{
“帐户[1]”:{“0”:“http:\/\/twitter.com\/sirgarick45”},
“image[1]:{“0”:“http:\/\/a0.twimg.com\/profile\u images\/3071585947\/207a70fcc09749a1359287a204db1c0b\u normal.jpeg”,
“姓名[1]:“迈克·格林”,
“推特[1]:“RT@RussellVogt:#NDAA美利坚合众国请查看!!http:\/\/t.co\/KxRx3fMH”,
“时间[1]:“1分钟前”
}
{
“帐户[2]”:{“0”:“http:\/\/twitter.com\/Gabriel\u Enrike”},
“图像[2]:{“0”:“http:\/\/a0.twimg.com\/profile\u images\/2921364087\/282FEC087888F1D96EB0D1167A2639A69\u normal.jpeg”,
“姓名[2]:“加布里埃尔·恩里克”,
“推特[2]:“@BarackObama将在马丁·路德·金纪念日宣誓就任第二届美国总统!#Hehadderam#TeamBO#YesWeCan”,
“时间[2]:“1分钟前”
}
{
“帐户[3]:{“0”:“http:\/\/twitter.com\/Davis\u T94”},
“image[3]:{“0”:“http:\/\/a0.twimg.com\/profile\u images\/2627985413\/image\u normal.jpg”,
“姓名[3]:“蒂姆·戴维斯”,
“推特[3]:“RT@FactBoook:美国曾经占领墨西哥城。”,
“时间[3]:“1分钟前”
}
这是我用来将字符串转换为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();
        }
private String[]acnt;
私有字符串[]img;
私有字符串[]nam;
专用字符串[]行波管;
私人字符串[]tim;
JSONObject对象j=null;
//创建jsonobject的响应字符串并获取所需值
试一试{
obj=新的JSONObject(twitter_响应);
int lenght=obj.length();
acnt=新字符串[长度];
img=新字符串[长度];
nam=新字符串[长度];
twt=新串[长度];
tim=新字符串[长度];
//对象getJSONArray(twitter_响应);
对于($i=0;$i

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

您的输入不是有效的JSON。JSON数组使用模式
[{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"
  },
  ...
]
[
{
“帐户:{“0”:“http:\/\/twitter.com\/OGTedBerg”},
“image”:{“0”:“http:\/\/a0.twimg.com\/profile\u images\/2821392281\/59f4fe1cbb44c8812401192a199b4b04\u normal.jpeg”,
“姓名”:“Ted Berg”,
“tweet”:“我知道我与美国联系最密切的口味是奶酪红辣椒。http:\/\/t.co\/5On9CQuV”,
“时间”:“1分钟前”
},
...
]

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

我认为您的JSON无效,要测试JSON的有效性,请使用@QAMAR thanku so Mory 4 reply和4 link…………感谢您的快速回复。。。我通过php制作json,这是我的代码“print json_encode(数组('account['.$i.]'=>$account,'image['.$i.]'=>$image,'name['.$i.]'=>$name,'tweet['.$i.]'=>$tweet,'time['.$i.]'=>$time));”我怎样才能更正呢?很遗憾,我无法帮助您使用PHP,但我已经用更多的细节编辑了我的答案。