anevins Posted September 28, 2012 Share Posted September 28, 2012 (edited) Hello, I'm trying to wrap a JSON array inside a JSON object but cannot figure out how. Here's the tutorial I'm following: In this example, you send a JSON array inside a JSON object's "data" field. Each item should have a field called "text". {"data": [{"text": "I love Titanic."}, {"text": "I hate Titanic."}]} This is my JSON array (var_dumped) string '["data",{"text":"Wrapped in my blanket always makes me feel a little better when I'm not feeling to well"},{"text":"@sportsology I know they are still far apart but I'm definitely feeling better"},{"text":"The way I'm feeling right now <<<<<"},{"text":"Sometimes my dad will sense when I'm feeling a bit shit and just give me a hug and it always makes me feel better. #appreciationtweet"},{"text":"@HayliMarie3 o no!!!!! I'm just now feeling better today!!! Suck down those emergencies!!!"},{"text'... (length=1005) This is my JSON object (var_dumped) string '"data"' (length=6) How can I go about this? Edited September 28, 2012 by anevins Quote Link to comment https://forums.phpfreaks.com/topic/268890-how-to-wrap-a-json-array-inside-a-json-object/ Share on other sites More sharing options...
anevins Posted September 28, 2012 Author Share Posted September 28, 2012 I'm using PHP to create that json array and object. Should I be posting on the PHP coding forum? Quote Link to comment https://forums.phpfreaks.com/topic/268890-how-to-wrap-a-json-array-inside-a-json-object/#findComment-1381619 Share on other sites More sharing options...
requinix Posted September 28, 2012 Share Posted September 28, 2012 (edited) The JSON you have doesn't match the JSON you're supposed to send so there's a problem with that too. What's your code? Edited September 28, 2012 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/268890-how-to-wrap-a-json-array-inside-a-json-object/#findComment-1381627 Share on other sites More sharing options...
anevins Posted September 28, 2012 Author Share Posted September 28, 2012 (edited) My code is function get140Sentiment ( $tweets ) { $url = ""; // $content = file_get_contents($url); $item_type = "data"; $text_array = array( $item_type ); foreach ( $tweets as $tweet ) { $text['text'] = $tweet->tweet_text; array_push($text_array, $text); } $json_object = ""; $json_object = json_encode ( $item_type, JSON_FORCE_OBJECT); $json_array = json_encode( $text_array ); var_dump($json_object, $json_array); } Edited September 28, 2012 by anevins Quote Link to comment https://forums.phpfreaks.com/topic/268890-how-to-wrap-a-json-array-inside-a-json-object/#findComment-1381632 Share on other sites More sharing options...
ManiacDan Posted September 28, 2012 Share Posted September 28, 2012 What...what are you wanting these items to be? As far as I can tell, json_object will be an object that just contains the string "data". This matches your output (but not what you say you want). json_array will be an array of all the twitter posts you're fetching from somwhere. This, again, matches your output. You probably want... function get140Sentiment ( $tweets ) { $item_type = "data"; $data = array('data' => array()); foreach ( $tweets as $tweet ) { $data['data'][] = array('text'=> $tweet->tweet_text); } $json_object = json_encode ( $data, JSON_FORCE_OBJECT); $json_array = json_encode( $data ); var_dump($json_object, $json_array); } Quote Link to comment https://forums.phpfreaks.com/topic/268890-how-to-wrap-a-json-array-inside-a-json-object/#findComment-1381636 Share on other sites More sharing options...
anevins Posted September 28, 2012 Author Share Posted September 28, 2012 Yes Maniac Dan, that code looks like it does what I want. Thanks for the input everyone. Quote Link to comment https://forums.phpfreaks.com/topic/268890-how-to-wrap-a-json-array-inside-a-json-object/#findComment-1381652 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.