I'm looking to train myself in JSON (request, response and parsing).
I'm trying to develop a simple application, parsing the response into a list as seen in this app (never mind the language, just see the screenshots)
However, JSON and parsing it looks like magic to me right now and I currently got no idea on how to use it. I've programmed a lot of c#, but markup languages and respones with tons of brackets, etc just confuses me.
Here you can see code snippets from the place I'm trying to parse. I'm going to use this as a people search app. Really need to work on my parsing skills.
I'm not asking you to do it for me, just point me to some easy tutorials and things like that.
Java equivalent code snippet from the site showing me the API:
package com.eniro.demo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.ObjectMapper;
public class EniroAPIExample {
private static ObjectMapper objectMapper = new ObjectMapper ();
public static void main(String[] args) throws JsonParseException, JsonProcessingException, IOException {
URL eniroApiUrl = new URL("http://api.eniro.com/cs/search/basic?profile=[profile]&key= [key]&country=se" +
"&version=1.1.3&search_word=pizza");
HttpURLConnection eniroApiConn = (HttpURLConnection) eniroApiUrl.openConnection();
if (eniroApiConn == null) {
return;
}
int respCode = eniroApiConn.getResponseCode();
if (respCode != 200) {
return;
}
BufferedReader in = new BufferedReader(new InputStreamReader(eniroApiConn.getInputStream(), "UTF-8"));
StringBuilder strResult = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null) {
strResult.append(inputLine);
}
in.close();
JsonNode json = objectMapper.readTree(objectMapper.getJsonFactory().createJsonParser(strResult.toString()));
System.out.println("Title: "+json.path("title"));
}
}
Edit:
I tried this code from this question:
private void search(string input)
{
// Perform the search here
Uri searchUri = new Uri("https://maps.googleapis.com/maps/api/geocode/json?address=1600%20Amphitheatre%20Parkway,%20Mountain%20View,%20CA&sensor=false");
try
{
WebClient web;
web = null;
web = new WebClient(); web.DownloadStringAsync(searchUri); web.DownloadStringCompleted += web_DownloadStringCompleted;
}
catch (WebException)
{
}
}
public class People
{
public List<Person> data { get; set; }
}
public class Person
{
public string id { get; set; }
public string name { get; set; }
public string phonenumber { get; set; }
public string address { get; set; }
}
But I don't really know where to go from there. I looked at this library, but I ain't getting anywhere
All the best, Erik