如何在 python 或 C# 中使用循环打印 json 响应的特定部分
How to print a specific part of a json response using a loop in python or in C#
我正在尝试打印 Json 响应的特定部分.. 但它不起作用..
这是完整的 json 回复:
{'hasError': False, 'errorDesc': None, 'result': '[{"itSpam":false,"register":false,"namecount":4,"ID":"xxxx","Name":"bb test","Phone":"00967xxxx","Country":"YE","userJob":null,"userEmail":null,"userPic":null,"isRegister":false,"isItSpam":false},{"itSpam":false,"register":false,"namecount":2,"ID":"xxxx","Name":"Test bb","Phone":"00967xxxx","Country":"YE","userJob":null,"userEmail":null,"userPic":null,"isRegister":false,"isItSpam":false},{"itSpam":false,"register":false,"namecount":2,"ID":"xxxx","Name":"bb bb bb","Phone":"00967xxxx","Country":"YE","userJob":null,"userEmail":null,"userPic":null,"isRegister":false,"isItSpam":false},{"itSpam":false,"register":false,"namecount":2,"ID":"xxxx","Name":"test bb","Phone":"00967xxxx","Country":"YE","userJob":null,"userEmail":null,"userPic":null,"isRegister":false,"isItSpam":false},{"itSpam":false,"register":false,"namecount":1,"ID":"xxxx","Name":"bb","Phone":"00967xxxx","Country":"YE","userJob":null,"userEmail":null,"userPic":null,"isRegister":false,"isItSpam":false}]'}
所以首先我确实选择了结果并且它完美地工作并且我得到了回应:
[{"itSpam":false,"register":false,"namecount":4,"ID":"xxxxx","Name":"xxxxx bb","Phone":"00967xxxxx","Country":"YE","userJob":null,"userEmail":null,"userPic":null,"isRegister":false,"isItSpam":false},{"itSpam":false,"register":false,"namecount":2,"ID":"xxxxx","Name":"testtbb","Phone":"00967xxxxx","Country":"YE","userJob":null,"userEmail":null,"userPic":null,"isRegister":false,"isItSpam":false},{"itSpam":false,"register":false,"namecount":2,"ID":"xxxxx","Name":"xxxxx xxxxx bb","Phone":"00967xxxxx","Country":"YE","userJob":null,"userEmail":null,"userPic":null,"isRegister":false,"isItSpam":false},{"itSpam":false,"register":false,"namecount":2,"ID":"xxxxx","Name":"bb bb","Phone":"00967xxxxx","Country":"YE","userJob":null,"userEmail":null,"userPic":null,"isRegister":false,"isItSpam":false},{"itSpam":false,"register":false,"namecount":1,"ID":"xxxxx","Name":"xxxxx","Phone":"00967xxxxx","Country":"YE","userJob":null,"userEmail":null,"userPic":null,"isRegister":false,"isItSpam":false}]
一切都很好......但是每当我试图在第二个 json 响应中打印所有“名称”时,我总是会出错......这是我的 python 代码:
response = requests.post(url, headers=headers, data=data)
res = response.json()
me = res['result']
for n in me:
mo = n['Name']
print(mo)
但是没用
所以我的问题是如何打印第二个响应中的所有“名称”值?
谢谢大家
首先,您必须通过将单引号替换为双引号来修复 json 响应字符串。在此之后,你必须在尝试做某事之前解析你的 json 字符串。这是使用 c#
打印所有名称的方法
using Newtonsoft.Json;
var json = response.Replace("'[","[").Replace("]'","]").Replace("'","\"").Replace("None","null").Replace("False","false");
var jsonParsed = JObject.Parse(json);
foreach (JObject item in jsonParsed["result"])
{
Console.WriteLine(item["Name"]);
}
结果
xxxxx bb
testtbb
xxxxx xxxxx bb
bb bb
xxxxx
注意单引号和双引号。
'result': '[{"itSpam":false,"
^--- the value of property result is string and not JSON
如果内部结果是字符串,您可以将其作为 JSON 加载,然后将其作为字典处理。这是 Python 使用列表推导的示例(保留了一些奇怪的变量命名):
inner_result = json.loads(me)
all_names = [item['Name'] for item in inner_result]
print(all_names)
我正在尝试打印 Json 响应的特定部分.. 但它不起作用..
这是完整的 json 回复:
{'hasError': False, 'errorDesc': None, 'result': '[{"itSpam":false,"register":false,"namecount":4,"ID":"xxxx","Name":"bb test","Phone":"00967xxxx","Country":"YE","userJob":null,"userEmail":null,"userPic":null,"isRegister":false,"isItSpam":false},{"itSpam":false,"register":false,"namecount":2,"ID":"xxxx","Name":"Test bb","Phone":"00967xxxx","Country":"YE","userJob":null,"userEmail":null,"userPic":null,"isRegister":false,"isItSpam":false},{"itSpam":false,"register":false,"namecount":2,"ID":"xxxx","Name":"bb bb bb","Phone":"00967xxxx","Country":"YE","userJob":null,"userEmail":null,"userPic":null,"isRegister":false,"isItSpam":false},{"itSpam":false,"register":false,"namecount":2,"ID":"xxxx","Name":"test bb","Phone":"00967xxxx","Country":"YE","userJob":null,"userEmail":null,"userPic":null,"isRegister":false,"isItSpam":false},{"itSpam":false,"register":false,"namecount":1,"ID":"xxxx","Name":"bb","Phone":"00967xxxx","Country":"YE","userJob":null,"userEmail":null,"userPic":null,"isRegister":false,"isItSpam":false}]'}
所以首先我确实选择了结果并且它完美地工作并且我得到了回应:
[{"itSpam":false,"register":false,"namecount":4,"ID":"xxxxx","Name":"xxxxx bb","Phone":"00967xxxxx","Country":"YE","userJob":null,"userEmail":null,"userPic":null,"isRegister":false,"isItSpam":false},{"itSpam":false,"register":false,"namecount":2,"ID":"xxxxx","Name":"testtbb","Phone":"00967xxxxx","Country":"YE","userJob":null,"userEmail":null,"userPic":null,"isRegister":false,"isItSpam":false},{"itSpam":false,"register":false,"namecount":2,"ID":"xxxxx","Name":"xxxxx xxxxx bb","Phone":"00967xxxxx","Country":"YE","userJob":null,"userEmail":null,"userPic":null,"isRegister":false,"isItSpam":false},{"itSpam":false,"register":false,"namecount":2,"ID":"xxxxx","Name":"bb bb","Phone":"00967xxxxx","Country":"YE","userJob":null,"userEmail":null,"userPic":null,"isRegister":false,"isItSpam":false},{"itSpam":false,"register":false,"namecount":1,"ID":"xxxxx","Name":"xxxxx","Phone":"00967xxxxx","Country":"YE","userJob":null,"userEmail":null,"userPic":null,"isRegister":false,"isItSpam":false}]
一切都很好......但是每当我试图在第二个 json 响应中打印所有“名称”时,我总是会出错......这是我的 python 代码:
response = requests.post(url, headers=headers, data=data)
res = response.json()
me = res['result']
for n in me:
mo = n['Name']
print(mo)
但是没用
所以我的问题是如何打印第二个响应中的所有“名称”值?
谢谢大家
首先,您必须通过将单引号替换为双引号来修复 json 响应字符串。在此之后,你必须在尝试做某事之前解析你的 json 字符串。这是使用 c#
打印所有名称的方法using Newtonsoft.Json;
var json = response.Replace("'[","[").Replace("]'","]").Replace("'","\"").Replace("None","null").Replace("False","false");
var jsonParsed = JObject.Parse(json);
foreach (JObject item in jsonParsed["result"])
{
Console.WriteLine(item["Name"]);
}
结果
xxxxx bb
testtbb
xxxxx xxxxx bb
bb bb
xxxxx
注意单引号和双引号。
'result': '[{"itSpam":false,"
^--- the value of property result is string and not JSON
如果内部结果是字符串,您可以将其作为 JSON 加载,然后将其作为字典处理。这是 Python 使用列表推导的示例(保留了一些奇怪的变量命名):
inner_result = json.loads(me)
all_names = [item['Name'] for item in inner_result]
print(all_names)