Guest, before posting your code please take these rules into consideration:
It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
Don't share a wall of code. All we want is the problem area, the code related to your issue.
1) Is it possible to do some sort of phrase system with json?
2) Will markdown work through json? (ex: { "faq": "[text](text link)
3) Can I have multiple user credentials for posting content? (EG: User 1 only posts content from user_1_twitter but user 2 post content from 3,4,5,6)
I am not a huge fan of either.
I think you should do something along the lines of the code below... This might not work 100% for what you are doing, but it's very important to mention.
When using JSON or an array or object, you should try to keep the format as consistent as possible. For example, if you have multiple services (twitter, reddit, etc) then you should format it so they all follow the same structure... So they all have name, client_id, etc - in the same place. In the example below, we use the data object with the subreddits array so that our code can use some special logic later on for reddit specifically.
However, even this affects the consistency a bit. All of a sudden the Twitter information is different than the Reddit information because of the special subreddits array. The reason I like to do things this way though is because you can basically say...
foreach services as service - or use some sort of loop to go through each service. Your loop won't care if you change the name of a service, so you do not need to reference it by name like services['reddit'] or services.reddit.
You can instead just say services[0] or something similar to access the first service.
The problem with your examples in my opinion is that the data is just wildly different for each. You have two services (twitter, reddit) and then a 'people' object at the bottom. It's unclear if this JSON is ideal because I don't know your exact use case, but generally speaking each JSON object should represent one data set in my opinion, or be structured properly so that names of services/things can change without affecting how they are referenced in the code.
If your code is pretty minimal & using all aspects of the JSON without looping and dynamically automating the process, you're probably fine, but for the future try to make sure that all of your data can be recursively used by your code even with changes in the future or more services being added.
So, let's pretend we want to loop over this list. Because I do not use Python I will use PHP for this, but it will be the same for python just with different syntax/language used... the CONCEPT is the same, so I hope this helps you better understand
PHP:
$services = json_decode($json, true); // ~ line 1
$numServices = count($services); // ~ line 2
for($loop = 0; $loop < $numServices; $loop++){ // ~ line 3
$data = file_get_contents($services[$loop]["base_url"]); // ~ line 4
echo "<h4>{$services[$loop]["name"]}</h4>"; // ~ line 5
echo $data; // ~ line 6
if(count($services[$loop]["data"]) > 0){ // ~ line 7
foreach($services[$loop]["data"] as $extraData){ // ~ line 8
$newData = file_get_contents($services[$loop]["base_url"] . $extraData); // ~ line 9
echo "<hr><u>$extraData:</u><br />$newData"; // ~ line 10
} // ~ line 11
} // ~ line 12
echo "<hr>"; // ~ line 13
} // line 14
echo "<br>looping is done"; // ~ line 15
In this code it does the following:
Line 1 takes a JSON object & turns it into a PHP array
Count the number of services in our array $services
Start a loop so we can go through each service the same way
$data is our variable to get the file contents of the service URL (base_url... ex: https://reddit.com). Normally our base_url would have something like "%API_KEY%" inside of it so that every service URL can have the "client_id" or "api_key" value added to the base_url dynamically
Echo out (in h4 tags) the service name
Line echos out the data from step 4. We wouldn't normally do this, but this is just an example
Check to see if this service has any data: inside of it (ex: subreddits)
If there's extra data targets, start new loop on line 8
Get the new file content... ex, in loop 1 we get the HTML from https://reddit.com, and then down on line 9 it'sactually going for the first data (first subreddit), so it's grabbing the HTML from: https://reddit.com/r/sports because that's our first data string, followed by r/news
On line 10 we just echo out a line break, underlined data name, and then the new data
On line 11 we close our little extra loop
Line 12 we close our if statement to see if service has extra target URLs to search (ex: on twitter loop through, or the 2nd loop, we would first grab html from https://twitter.com, followed by grabbing the html for profile https://twitter.com/wuburdev )
On line 13 we add a new line break
on line 14 we completely stop looping the services
On line 15 we echo out that looping is done
This example will work for an initial URL (ex: twitter.com) and any "sub page" (ex: subreddit, twitter profile, etc) that you want to search.
You could obviously change up the data or base URL to do a lot of things.... use the .JSON url on reddit to grab json data from reddit instead of raw html... or even add CodeForum into your json object to crawl CF pages!
1) Is it possible to do some sort of phrase system with json?
2) Will markdown work through json? (ex: { "faq": "[text](text link)
3) Can I have multiple user credentials for posting content? (EG: User 1 only posts content from user_1_twitter but user 2 post content from 3,4,5,6)
links = '{"faq":"https://google.com","other":"https://codeforum.org"}'
# the links JSON would come from something else obviously, like retreiving from API or a .json file
import json
usejson = json.loads(links)
print(usejson["faq"])
print("The other link is " + usejson["other"])
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
www.w3schools.com
3) Yes, you could do that. You would need to set up some sort of relationship data...
So you could have user1 attached to an array of twitters, like so (this is just Python now, not json below):
Python:
users = {
"user1":[0],
"user2":[1,2,3,4,5]
}
twitters = [
"content0",
"content1",
"content2",
"content3",
"content4",
"content5",
]
for user, contents in users.items(): # loop each user-content in users
print(user + " Content:") # display which user we're on
for content in contents: # loop through the content IDs in users data
print(twitters[content]) # grab the content from the twitters data
# replace prints with actual code to do stuff with it :)