We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,124 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Adapted way of using PHP to output JSON assets

Hi everyone,

Let me first explain the idea:
I have a JSON document which has the following structure:

{
        "timeline":
        {
            "headline":"Example Content",
            "type":"default",
            "text":"Example Content",
            "startDate":"2013,1,1",
            "endDate":"2013,1,28",
            "date": [
                {
                    "startDate":"2013,1,10",
                    "headline":"Example Content",
                    "text":"Example Content",
                    "asset":
                    {
                        "media":"Example Content",
                        "credit":"",
                        "caption":""
                    }
                },
                {
                    "startDate":"2013,1,14",
                    "headline":"Example Content",
                    "text":"Example Content",
                    "asset":
                    {
                        "media":"Example Content",
                        "credit":"",
                        "caption":""
                    }
                },
                {
                    "startDate":"2013,1,21",
                    "headline":"Example Content",
                    "text":"Example Content",
                    "asset":
                    {
                        "media":"Example Content",
                        "credit":"",
                        "caption":""
                    }
                }
            ]
        }
    }

I want to be able to add the following sections using PHP that will get data from a database:

            {
                "startDate":"2013,1,14",
                "headline":"Example Content",
                "text":"Example Content",
                "asset":
                {
                    "media":"Example Content",
                    "credit":"",
                    "caption":""
                }
            },

Now people have been talking about json_encode() however, I am not quite sure 1) how to do this to achieve what I want and 2) whether that method would allow me to do what I want.

So basically could someone provide some help as to how to do it using json_encode() or if anyone knows of an alternative way, I would be happy to take all advice.

Thanks a bunch.
Jack

3
Contributors
3
Replies
32 Minutes
Discussion Span
3 Months Ago
Last Updated
5
Views
scaasiboi
Junior Poster in Training
52 posts since Oct 2010
Reputation Points: 10
Solved Threads: 3
Skill Endorsements: 0

You will need to use json_decode to create an array of your JSON. Then you can add your new data to that array. Finally use json_encode to create the new JSON.

pritaeas
Posting Prodigy
Moderator
9,293 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,462
Skill Endorsements: 86

How do you do this, I have never worked with json_decode or json_encode.

Thanks for replying.

scaasiboi
Junior Poster in Training
52 posts since Oct 2010
Reputation Points: 10
Solved Threads: 3
Skill Endorsements: 0
AleMonteiro
Master Poster
752 posts since Aug 2010
Reputation Points: 129
Solved Threads: 140
Skill Endorsements: 23

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0660 seconds using 2.71MB