Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: JSON Decoding help

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question JSON Decoding help

    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);
    			}
     
     
    	}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JSON Decoding help

    Exception in thread "main" java.lang.NullPointerException
    at mtgox.main(mtgox.java:45).
    Look at line 45 in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable it is, add a println just before line 45 and print out the values of all the varibles on that line.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JSON Decoding help

    It is saying that my line variable is null when I try to decode it, but when I do println it prints out the whole JSON object and I only need the "high" and "low".

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JSON Decoding help

    It is saying that my line variable is null
    Can you post the source line that you are talking about? line 45 where the NPE occurred.

    I do println it prints out the whole JSON object
    The variable: line is defined as a String. How does an object get printed? I'd expect to see the content of the line variable which would be a String.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. STRUTS2 with JSON
    By karthikkarnati in forum Web Frameworks
    Replies: 1
    Last Post: May 9th, 2012, 09:53 AM
  2. JSON reader
    By Rootntootn in forum Java Theory & Questions
    Replies: 4
    Last Post: April 9th, 2012, 09:14 AM
  3. PHP Developer - OOP / MVC / XML / JSON - £40,000 (NEG)
    By JacquelineRobbins in forum Paid Java Projects
    Replies: 0
    Last Post: January 12th, 2012, 06:58 AM
  4. Combing/Decoding MPEG and Other Video Format?
    By MrFish in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 5th, 2011, 08:21 AM
  5. Replies: 5
    Last Post: October 12th, 2009, 12:24 AM