0

{"result": 204, "id": "1", "jsonrpc": "2.0"}

this is the result of a json-rpc request for total count of the database. so the total number of documents in the databse is 204. how to grab the value 204 and store it in a variable so that i can use it to perform some arithmetic operations?

sanjay
  • 3
  • 5

3 Answers3

0

you can use this code[in php cause didn't say which language to use]:

$array = json_decode('{"result": 204, "id": "1", "jsonrpc": "2.0"}' , true);
$result = $array["result"];
echp $result;

the output will be this:

240
TheFaultInOurStars
  • 3,464
  • 1
  • 8
  • 29
0

If you do the request with angular http client, the default behaviour is that you get back the response body as a json object... So you could just read it's properties, i.e.:

http.get('test.de').subscribe( (res) => { let test = res.result}); 
console.log(test);

So what language/framework are you using?

If you get the response object as described in our comment via "res.json()" it would be the same, you could read it's properties... I don't think it is necessary to do a json decode because it already should be a json object. If you are writing in TypeScript the compiler may not like that he does not know the definition of the object, but you could just cast the response type to be any, object, or json and it should work!

EDIT: Because it is AngularJS and not Angular2+ the response body is in a property called "data" of the angular http response object and types don't matter because it is plain js, so the call should look like this:

http.get('test.de').subscribe( (res) => { let test = res.data.result});
console.log(test); // => Output: 204

EDIT2: If it is the case that I got you wrong and you're writing in typescript and you are using a current angular release and not angularJS you would have to cast to any first! But it would be better to create an interface or class which is describing the response...

Not so pretty way:

http.get<any>('test.de').subscribe( (res) => { let test = res.result});

OR

http.get('test.de').subscribe( (res: any) => { let test = res.result});

Prettier way:

interface BasicApiResponse {
    result: JSON | string | number | any; // => here it depends on the specific request...
    id: number;
    jsonrpc: string;
}

Then you could do s.th. like:

http.get<BasicApiResopnse>('test.de').subscribe( (res: BasicApiResopnse) => { let test = res.result});
ToTaTaRi
  • 311
  • 1
  • 8
  • i'm using angularjs. this.http.post('http://localhost:80/jsonrpc', data).subscribe( (res) => { let test = res.result}); i'm getting error that the 'property result does not exist on the Response' – sanjay Mar 09 '20 at 06:16
  • I'm more familiar with Angular2+, but I would guess that it is not a big difference with AngularJS, do you get an error? Just look up the docs, they say that a json object is deserialized by default: https://docs.angularjs.org/api/ng/service/$http# – ToTaTaRi Mar 09 '20 at 06:20
  • Ah, I see that the response body seems to be in a property "data" of the response object, so your call should be like: res.data.result Types won't matter because it is plain js! – ToTaTaRi Mar 09 '20 at 06:23
  • const data = { "jsonrpc" : "2.0", "method" : "count", "id" : "1", "params": {'argument' : 'something' } }; this.http.post('http://localhost:80/jsonrpc', data).subscribe( (res) => { let test = res.data.result}); so this how i'm making a request, can please tell me how to do it in this situation – sanjay Mar 09 '20 at 06:30
  • Sorry I don't really get what you mean, now you have your result in the variable "test" don't you!? You could also save it to "this.json1" as in your example! this.http.post('localhost:80/jsonrpc', data).subscribe( (res) => { this.json1 = res.data.result}); – ToTaTaRi Mar 09 '20 at 06:34
  • when i type res.data.result , i'm getting a eror that property 'data' does not exist on the type Response. – sanjay Mar 09 '20 at 06:48
  • are you sure you are using angularJS so the first angular version and not a current release like angular7 or 8? When it is typescript you must cast to any! But if your are using ts it would be better to make an interface or class which is discribing the result and cast to that one! – ToTaTaRi Mar 09 '20 at 06:56
0

It is not clear from your question if you're using Angular or AngularJS. Because there is a clear distinction when it comes to data handling between those versions. Based on your comments on @ToTaTaRi answer, it looks like you might be using AngularJS. In which case, try the following code

$http.get("<your url here>")
.then(function(response) { // handle response
  $scope.result = response.data.result;
}, function(response) { // handle error
  $scope.content = "Something went wrong";
});

If you are still not sure if the result value is of type number, then cast it from string to number using this answer.

ruth
  • 29,535
  • 4
  • 30
  • 57