I am trying to print a specific part of a Json response .. but It dose not work ..
here is the whole json response:
{'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}]'}
so first I did choose result and it works perfectly and I got the response:
[{"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}]
and all is fine ... but whenever I tried to print all "Name" in the second json response I always get errors ... this is my python code:
response = requests.post(url, headers=headers, data=data)
res = response.json()
me = res['result']
for n in me:
mo = n['Name']
print(mo)
but it did not worked
so my question is how to print all "Name" values from the second response?
Thanks all.
CodePudding user response:
Pay attention to single and double quotes.
'result': '[{"itSpam":false,"
^--- the value of property result is string and not JSON
If it's intended that inner result is string you can load it as JSON and then process it as dictionary. Here is Python example using list comprehension (keeping a little bizarre variable naming):
inner_result = json.loads(me)
all_names = [item['Name'] for item in inner_result]
print(all_names)
CodePudding user response:
At first you have to fix your json response string by replacing single quote by doubles. After this you have to parse your json string before trying to do something. This is how you can print all names using 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"]);
}
result
xxxxx bb
testtbb
xxxxx xxxxx bb
bb bb
xxxxx