0

I'm creating JSON on server through PHP like this (and if I'm creating JSON wrong please tell me how can I make this correct). Now I want to convert this string into JSON object on Android.

{
 "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"
  }

and this is my Java code that I'm using to convert string to JSON object.

 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();
        }

With this I'm getting only first value; any idea what I'm doing wrong?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Zeeshan
  • 1,625
  • 17
  • 25

1 Answers1

0

Your input isn't valid JSON. JSON arrays use the pattern [ { element }, { element }, { element } ...], not the bracket syntax that you're using.

Here's a related question about how to format an array in JSON, and here's some precise documentation about JSON syntax in general.

EDIT: I will say that the JSON sample that you posted is pretty close to being valid (in fact, each individual block is valid on its own, it's just not valid if you're trying to parse the whole set as-is). That said, I'd still argue that it might make more sense to format your data in a more traditional JSON array structure, such as:

[
  {
    "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"
  },
  ...
]

That would probably make the parsing process quite a bit simpler.

Community
  • 1
  • 1
Cloudy
  • 2,401
  • 25
  • 28
  • thanks for quick reply... i m making that json through php and this is my code 'print json_encode(array('account['.$i.']' => $account, 'image['.$i.']' => $image, 'name['.$i.']' => $name, 'tweet['.$i.']' => $tweet, 'time['.$i.']' => $time) );' how can i make it correct – Zeeshan Jan 09 '13 at 17:33
  • I can't help you with your PHP, unfortunately, but I've edited my answer with some more details. – Cloudy Jan 09 '13 at 17:45