# [Java] Objectmapper can't read Json String correctly



## nzall (Jan 9, 2012)

I'm using the Spring Framework to retrieve Google +1 count from a hidden API as explained on Google +1 Button SEO - Getting Bulk Counts with the hidden API - Tom Anthony.

I currently have the following code:


```
public class GoogleProvider implements SocialMediaProvider {
	public RestTemplate restTemplate = new RestTemplate();
	public String url = "https://clients6.google.com/rpc?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ";

	@Override
	public Object RetrieveShares(String URL) {
		return null;

	}

	public int Search(String uRL) throws JsonProcessingException, IOException {
		ObjectMapper mapper = new ObjectMapper();
		System.out.println("objectmapper gemaakt");
		String Json1 = "[{\"method\":\"pos.plusones.get\",\"id\":\"p\",\"params\":{\"nolog\":true,\"id\":\"";
		String Json2 = "\",\"source\":\"widget\",\"userId\":\"@viewer\",\"groupId\":\"@self\"},\"jsonrpc\":\"2.0\",\"key\":\"p\",\"apiVersion\":\"v1\"}]";
		String Json3 = Json1 + uRL + Json2;
		JsonNode urlNode = mapper
					.readTree(Json3);

		int plusOnes = 0;
		JsonNode responseString = mapper.readTree(restTemplate.postForLocation(url, urlNode).toURL());
		plusOnes = responseString.get(0).get("result").get("metadata")
				.get("globalCounts").get("count").getIntValue();
		return plusOnes;
	}

	@Override
	public void searchForURL(String URL) {
	}

	@Override
	public void printResult(Object O) {

	}

	@Override
	public void connect() {

	}

}
```
the problem i'm having is that it's giving a NullPointerException on line 40, which is the second mapper.readTree() call. When I remove the throws declaration, it gives me an error concerning unhandled exception type JsonProcessingException, which leads me to believe my JSON string is formatted incorrectly, which likely has to do with having to escape so many characters.


```
'[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"' . $url . '","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]'
```
this is the original JSON string from the PHP example. If at all possible, i'd like to:

not create any extra classes;
not call PHP code from Java.


----------



## nzall (Jan 9, 2012)

I've solved this problem. turns out the problem wasn't with incorrectly formatted JSON, or with a communication error, but i was trying to wrap a JSON object into another JSON object, which caused issues. I also wasn't passing enough parameters to one of the functions.

the fixed code is: 


```
public int Search(String uRL) throws JsonProcessingException, IOException {
		ObjectMapper mapper = new ObjectMapper();
		mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
		System.out.println("objectmapper gemaakt");
		String Json1 = "[{\"method\":\"pos.plusones.get\",\"id\":\"p\",\"params\":{\"nolog\":true,\"id\":\"";
		String Json2 = "\",\"source\":\"widget\",\"userId\":\"@viewer\",\"groupId\":\"@self\"},\"jsonrpc\":\"2.0\",\"key\":\"p\",\"apiVersion\":\"v1\"}]";
		String Json3 = Json1 + uRL + Json2;
		JsonNode urlNode = mapper.readTree(Json3);
		int plusOnes = 0;
		JsonNode responseString = restTemplate.postForObject(googleURL,
				urlNode, JsonNode.class);
		System.out.println("verbonden met google");
		plusOnes = responseString.getElements().next().get("result")
				.get("metadata").get("globalCounts").get("count").getIntValue();
		return plusOnes;
	}
```


----------

