I got the .jar from json-simple - JSON.simple - A simple Java toolkit for JSON - Google Project Hosting that I added to my Java build path in eclipse. The example works and now I am trying to decode a JSON object from https://mtgox.com/api/0/data/ticker.php but I get an error : Exception in thread "main" java.lang.NullPointerException
at mtgox.main(mtgox.java:45). Thanks for you help my code is posted below:
I changed it back to the example to did compile correctly, but my problem is when i add the line variable to replace string s.
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.JSONValue; public class mtgox { public static void main(String[] args) { HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("https://mtgox.com/api/0/data/ticker.php"); try { List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); nameValuePairs.add(new BasicNameValuePair("registrationid", "123456789")); post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = client.execute(post); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String line = ""; while ((line = rd.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } System.out.println("=======decode======="); String s="[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]"; Object obj=JSONValue.parse(s); JSONArray array=(JSONArray)obj; System.out.println("======the 2nd element of array======"); System.out.println(array.get(1)); System.out.println(); JSONObject obj2=(JSONObject)array.get(1); System.out.println("======field \"1\"=========="); System.out.println(obj2.get("1")); s="{}"; obj=JSONValue.parse(s); System.out.println(obj); s="[1,]"; obj=JSONValue.parse(s); System.out.println(obj); s="[5,,2]"; obj=JSONValue.parse(s); System.out.println(obj); } }