Reputation: 21
I've been trying for the last couple hours to fix this. I'm a little rusty when it comes to Java and decided I wanted to finish this method where I'm trying to parse the json to get the name of a map.
private static void mapLookUp (String mapId){
HttpClient httpclient = HttpClients.createDefault();
try
{
URIBuilder builder = new URIBuilder("https://www.haloapi.com/metadata/h5/metadata/maps");
URI uri = builder.build();
HttpGetWithEntity request = new HttpGetWithEntity(uri);
request.addHeader("ocp-apim-subscription-key", "aa09014c153b4a4b9c3a4937356e208a");
// Request body
StringEntity reqEntity = new StringEntity("{body}");
request.setEntity(reqEntity);
HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null)
{
String response2request = EntityUtils.toString(entity);
//System.out.println(response2request.length()+"\n"+response2request);
String jsonString = "{\"Results\":"+response2request+"}";
System.out.println(jsonString);
JSONObject jsonResult = new JSONObject(jsonString);
List<String> mapName = new ArrayList<String>();
List<String> mapIds = new ArrayList<String>();
JSONArray array = jsonResult.getJSONArray("Results");
for(int i = 0 ; i < array.length() ; i++){
mapName.add(array.getJSONObject(i).getString("name"));
mapIds.add(array.getJSONObject(i).getString("id"));}
for(int i = 0 ; i < mapIds.size() ; i++)
if(mapIds.get(i).equals(mapId))
System.out.println("The most recent game was on "+mapName.get(i));
}
else
System.out.println("NULL");
}
catch (Exception e)
{
System.out.println("Caught exception");
System.out.println(e.getMessage());
}
}
In the output I get JSONObject["name"] not a string.
Upvotes: 2
Views: 1438
Reputation: 4120
check JSON source. It seems like it may have no " around name value, or name is an object. as example something like:
...
"name":John Doe,
...
or
"name":{"first":"John", "last":"Doe"},
...
BTW: Second is more expected. First must fail before, because it is wrong JSON. Value with no " around must be a number. But maybe name is empty like:
...
"name":,
...
Upvotes: 1