Python - 在一个API调用中POST多个记录

时间:2017-06-22 17:39:36

标签: python json api

我正在使用带有API的Python 3来发送像这样的单个JSON有效负载,但工作正常但很慢,因为我有200个要定期发送的项目:

{
  "first_name":"Ellen",
  "address_country":"US"
}

我已经检查过,我可以手动发送这个有效负载,它会创建两条我需要的记录:

{
    ["first_name":"Ellen",
    "address_country":"US"],

    ["first_name":"John",
    "address_country":"US"]

}

但我似乎无法以编程方式生成多个记录。目前我的代码是:

payload = {}
payload['first_name'] = "Ellen"
payload['address_country'] = "US"
json_payload = json.dumps(payload)

如何向有效负载添加第二条记录,我会一直覆盖它...我本质上想要遍历我的数组来构建我的有效负载并立即发送x个结果而不是每个数组迭代发送一个有效载荷。

好的,所以感谢所有回复但却没有解决问题的人,我得到了一个JSON有效载荷,看起来就像下面我认为你的意思:

[
    {"first_name":"Ellen",
    "address_country":"US"},

    {"first_name":"John",
    "address_country":"US"}

]

但服务器会返回错误代码400,表示没有字段(它们是)。我假设他们的API每个请求只能接受一个输入..: - (

1 个答案:

答案 0 :(得分:1)

你只需要使用正确的标志:)

Uri alertSound = Uri.parse("android.resource://" + ctx.getPackageName() + "/raw/page_the_doctor");

        //CLICK ON NOTIFICATION
        Intent notificationIntent = new Intent(ctx, PushNotificationActions.class).setAction(ACCEPT_EXAM).putExtra("visitId", visitId).putExtra("link", link);
        PendingIntent clickIntent = PendingIntent.getBroadcast(ctx, Integer.parseInt(visitId), notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(ctx)
                .setAutoCancel(true)
                .setSmallIcon(R.mipmap.telemed_logo)
                .setContentTitle("TELEMED PATIENT READY")
                .setContentText(notification)
                .setDefaults( Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS)
                .setSound( alertSound )
                .setPriority(Notification.PRIORITY_MAX)
                .setContentIntent(clickIntent)
                .setAutoCancel(true);

        //Accept intent
        Intent acceptExam = new Intent(ctx, PushNotificationActions.class).setAction(ACCEPT_EXAM).putExtra("visitId", visitId).putExtra("link", link);
        PendingIntent pendingAcceptIntent = PendingIntent.getBroadcast(ctx, Integer.parseInt(visitId), acceptExam, PendingIntent.FLAG_UPDATE_CURRENT);
        notificationBuilder.addAction(R.drawable.accept_action_24dp, "ACCEPT", pendingAcceptIntent);

        //Dismiss intent
        Intent dismissExam = new Intent(ctx, PushNotificationActions.class).setAction(DISMISS_EXAM).putExtra("visitId", visitId).putExtra("link", link);
        PendingIntent pendingDismissIntent = PendingIntent.getBroadcast(ctx, Integer.parseInt(visitId), dismissExam, PendingIntent.FLAG_UPDATE_CURRENT);
        notificationBuilder.addAction(R.drawable.dismiss_action_24dp, "DISMISS", pendingDismissIntent);

        Notification noti = notificationBuilder.build();
        noti.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;

        NotificationManager mNotificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(Integer.parseInt(visitId), noti);

逐行进行:

[
    {"first_name":"Ellen",
    "address_country":"US"},
    {"first_name":"John",
    "address_country":"US"}
]

甚至:

import json
payload = []
current_person = {}
current_person['first_name'] = 'Ellen'
current_person['address_country'] = 'US'
payload.append(current_person)
current_person['first_name'] = 'Ronnie'
current_person['address_country'] = 'Canada'
payload.append(current_person)
json_payload = json.dumps(payload)