简体   繁体   English

如何将由对象列表组成的字符串转换为 json

[英]How to convert a string made of list of objects to json

I am working with flask and I use Tweepy to get some tweets sent to the static folder and manipulate the result with it.我正在使用 flask 并使用 Tweepy 将一些推文发送到 static 文件夹并使用它来处理结果。

The problem I encounter it returns a string which is an array of objects.我遇到的问题是它返回一个字符串,它是一个对象数组。 If I use JSON.parse() is going to give me an error such as unexpected token ' in JSON at position 2 in JSON.parse如果我使用JSON.parse()会给我一个错误,例如unexpected token ' in JSON at position 2 in JSON.parse

[{1:a, name:name, etc...}, {1:a, name:name, etc...}, {1:a, name:name, etc...}, {1:a, name:name, etc...}, {1:a, name:name, etc...},]

I got this string from:我从以下位置得到了这个字符串:

twitter_search.py twitter_search.py

...
    for tweet in tweets:
           
            # data.append({"tweet": tweet.full_text})
            data.append({"time": str(tweet.created_at), "user" : str(tweet.user.screen_name), "tweet": str(tweet.full_text)})
...

in app.py在应用程序.py

@app.route('/')
def index():
    ip = supp.get_data('https://freegeoip.app/json/')
    iptojson = json.loads(ip.content)
    sctojson = ip.sc
    
    data = twitter_search.twsejs()
    
    s1 = json.dumps(data)
    d2 = json.loads(s1)

    return render_template('index.html',data=d2, iptojson = iptojson, sctojson = sctojson)

But it is a string and I need to use it as a JSON what can I do?但它是一个字符串,我需要将它用作 JSON 我该怎么办? I tried everything JSON.dumps dump parse can't figure it out我什么都试过了 JSON.dumps dump parse 搞不明白

This is an example of a get req这是获取请求的示例

[{'time': '2022-03-16 19:24:07+00:00', 'user': 'AnnBrock432', 'tweet': 'Yes, I\'m primarily on here for GAWs. Not because I\'m a "bad person" I\'m frozen w/ anxiety over possible homelessness. I\'d like to be in the mindset to socialize, but even in real time it\'s hard rn. Depression is real. If I had a solution to pressing matters it would change things'}, {'time': '2022-03-16 19:24:04+00:00', 'user': 'SiameseCatsMom', 'tweet': 'I must do something\nDepression and back pain  has me just like a slug in bed'}, {'time': '2022-03-16 19:24:02+00:00', 'user': 'TheCrabyMermade', 'tweet': 'My depression is so bad that even my two mile walk in the morning doesn’t give me nearly as much of the feel good hormones that everyone raves about. I just feel slightly madder bcuz here I am working out & not feeling the effects so why am I even walking 2 miles on the morning?!'}, {'time': '2022-03-16 19:24:02+00:00', 'user': 'AntiMysterious', 'tweet': "@jorgemarqueza @krisjw2630 @seanhannity Sorry buddy. It doesn't matter. Obama had to deal with the biggest recession since the great depression and all you guys did was cry about his spending. \n\nSo you guys are not only failures but you are hypocrites. Lol."}, {'time': '2022-03-16 19:24:01+00:00', 'user': 'TheDuke256', 'tweet': 'RT @Chiomy_mi: This country is so hard & messed up rn! \nAlot of people out there are going through tough time, hence many have fall into de…'}, {'time': '2022-03-16 19:24:01+00:00', 'user': 'Triphighalways1', 'tweet': 'Transcend the multiverse and easy #depression with #lsd and #shrooms link'}, {'time': '2022-03-16 19:23:59+00:00', 'user': 'i_depress_me', 'tweet': 'One of the upsides of crippling depression is that you occasionally get to have a box of Girl Scout cookies for breakfast.'}, {'time': '2022-03-16 19:23:59+00:00', 'user': '0bviouSquirre1', 'tweet': "in realistic terms it has been two and a half months since the cold weather set in such that i couldn't ride my bike\n\nseasonal depression has me acting like i've been locked inside for six months minimum, good lord"}, {'time': '2022-03-16 19:23:58+00:00', 'user': 'sunshiineo_o', 'tweet': "@OthelloVen that's OK for real 😭😭 idk where i sit but it might be on the depression side of the spectrum"}]

Making a request to an API comports to receive all sorts of responses.向 API 发出请求可以收到各种响应。 In order to have a well-formatted JSON, all I had to do is use a regex and avoid things such as double quotes, special characters, and brake lines.为了获得格式良好的 JSON,我所要做的就是使用正则表达式并避免使用双引号、特殊字符和刹车线等内容。 This made me have a cleaner JSON.这让我有了一个更清洁的 JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM