现在我正在做这件事:
self.response.headers['Content-Type'] = 'application/json' self.response.out.write('{"success": "some var", "payload": "some var"}')
使用某个库有没有更好的方法呢?
答案 0 :(得分:59)
是的,您应该使用Python 2.7支持的json
library:
import json
self.response.headers['Content-Type'] = 'application/json'
obj = {
'success': 'some var',
'payload': 'some var',
}
self.response.out.write(json.dumps(obj))
答案 1 :(得分:31)
webapp2
为json模块提供了一个方便的包装:如果可用,它将使用simplejson,如果可用,则使用Python> = 2.6的json模块,以及作为最后一个资源的django.utils.simplejson模块App Engine。
http://webapp2.readthedocs.io/en/latest/api/webapp2_extras/json.html
from webapp2_extras import json
self.response.content_type = 'application/json'
obj = {
'success': 'some var',
'payload': 'some var',
}
self.response.write(json.encode(obj))
答案 2 :(得分:12)
python本身有一个json module,它将确保您的JSON格式正确,手写JSON更容易出错。
import json
self.response.headers['Content-Type'] = 'application/json'
json.dump({"success":somevar,"payload":someothervar},self.response.out)
答案 3 :(得分:3)
我通常这样使用:
import java.util.Scanner;
public class ConvPhoneNumTwo{
public static void main(String[] args){
String s = getInput();
printPhone(convertToDigit(s));
}
public static String getInput() {
Scanner scan = new Scanner(System.in);
String s1 = "";
boolean length = false;
while (length==false){
System.out.println(" Please enter a telephone number in this format ###-AAA-AAAA") ;
s1 = scan.nextLine();
if(s1.length() != 12 ){
System.out.println( "this is an invalid choice try again Plz...");
length = false;
}
else{
length = true;
}
}
return s1;
}
public static String convertToDigit(String s010){
s010 = s010.toLowerCase();
String s001= s010.substring(0,3);
String s002 = s010.substring(4,6);
String s003 = s010.substring(8,12);
String s1 = (s001+s002+s003);
String s2 = "";
// Exceptions to our problem to stop invalid inputs
if (s1 == null) {
System.out.print (" invalid input null thing");
}
if (s1.length() != 10){
System.out.print (" invalid input");
}
String s6 = "";
for (int i=0; i < s1.length(); i++) {
if (Character.isDigit(s1.charAt(i))){
s2 += s1.charAt(i);
}
//sorting of the letter inputs
char ch = s1.charAt(i);
if (ch == 'a'||ch=='b'||ch== 'c'){
s2 += "2";
}
if (ch == 'd'||ch=='e'||ch=='f'){
s2 += "3";
}
if (ch == 'g'||ch=='h'||ch=='i'){
s2 += "4";
}
if (ch == 'j'||ch=='k'||ch=='l'){
s2 += "5";
}
if (ch == 'm'||ch=='n'||ch=='o'){
s2 += "6";
}
if (ch == 'p'||ch=='q'||ch=='r'|| ch=='s'){
s2 += "7";
}
if (ch == 't'||ch=='u'||ch=='v'){
s2 += "8";
}
if (ch == 'w'||ch=='x'||ch=='y'|| ch=='z')
{
s2 += "9";
}
else{
}
String s3 = s2.substring(0,3);
String s4 = s2.substring(3,6);
String s5 = s2.substring(6);
s6 = ( s3 +"-"+ s4 + "-"+ s5);
}
return s6;
}
public static void printPhone(String message) { //print out whatever string it is given, basically System.out.println(), but through a method instead
System.out.println(message);
}
}
答案 4 :(得分:1)
import json
import webapp2
def jsonify(**kwargs):
response = webapp2.Response(content_type="application/json")
json.dump(kwargs, response.out)
return response
你想要回复json回复的每个地方......
return jsonify(arg1='val1', arg2='val2')
或
return jsonify({ 'arg1': 'val1', 'arg2': 'val2' })