Thread: Java JSON

Results 1 to 8 of 8
  1. #1 Java JSON 
    Ich bin ein Sicherheitsexperte
    K2's Avatar
    Join Date
    Oct 2010
    Posts
    802
    Thanks given
    801
    Thanks received
    193
    Rep Power
    403
    Hi, I need help with this as I cannot find out how on earth to do it. Here is a example of what I want to do:

    Code:
    ...
    	/**
    	 * Gets the parameter.
    	 * @param parameter The parameter.
    	 * @return The paramter's value.
    	 */
    	@SuppressWarnings("unchecked")
    	public <E extends Object> E getParameter(String parameter) {
    		E returnz = null;
    		try {
    			returnz = (E) packet.getParameters().get(parameter);
    		} catch (JSONException e) {
    			e.printStackTrace();
    		}
    		return returnz;
    	}
    
    ...
    getParameters() is a extension of a JSONObject
    get returns a Object.

    I want to allow its use like so

    Code:
    ...
    int id = getParameter("myint");
    ...
    This would obviously throw a cannot cast integer to string bla bla, how would I go about this?

    - I do not want to do Integer.parseInt

    Thanks
    Respectfully,

    Steve Kreitzer
    Engineer, Blackspoke
    [email protected]
    "Those who care will always make a way."
    Reply With Quote  
     

  2. #2  
    Ich bin ein Sicherheitsexperte
    K2's Avatar
    Join Date
    Oct 2010
    Posts
    802
    Thanks given
    801
    Thanks received
    193
    Rep Power
    403
    No, I'm not using YAML, I'm using JSON.
    Respectfully,

    Steve Kreitzer
    Engineer, Blackspoke
    [email protected]
    "Those who care will always make a way."
    Reply With Quote  
     

  3. #3  
    Renown Programmer

    Join Date
    Dec 2010
    Posts
    2,876
    Thanks given
    508
    Thanks received
    1,898
    Rep Power
    5000
    i misread your post
    never talk to me or my wife's son ever again
    Reply With Quote  
     

  4. #4  
    Ich bin ein Sicherheitsexperte
    K2's Avatar
    Join Date
    Oct 2010
    Posts
    802
    Thanks given
    801
    Thanks received
    193
    Rep Power
    403
    That would require rewriting my whole frontend, which I don't want to do, I would much rather use json.

    Reason that I want to be able to print json back to the browser, and that can be read by some javascript.
    Respectfully,

    Steve Kreitzer
    Engineer, Blackspoke
    [email protected]
    "Those who care will always make a way."
    Reply With Quote  
     

  5. #5  
    Renown Programmer

    Join Date
    Dec 2010
    Posts
    2,876
    Thanks given
    508
    Thanks received
    1,898
    Rep Power
    5000
    https://code.google.com/p/google-gson/ may be your solution then (GSON.toJson and GSON.fromJson), although i don't see why your code wouldn't work
    never talk to me or my wife's son ever again
    Reply With Quote  
     

  6. #6  
    Renown Programmer
    veer's Avatar
    Join Date
    Nov 2007
    Posts
    3,746
    Thanks given
    354
    Thanks received
    1,370
    Rep Power
    3032
    taking my advice on using a markup for protocol definitions eh?

    if i understand your question correctly, then packet.getParameters() is an instance of JSONObject... why wouldn't parameters.getInt("myint") work?
    in the case that you want to maintain some constant interface for all config, you could just do...
    Code:
    int id = this.<Integer>getParameter("myid");
    to demonstrate:
    Code:
    import java.util.Map;
    import java.util.HashMap;
    
    class Mock {
    
        private Map parameters = new HashMap();
    
        private <E> E getParameter(String key) {
            return (E) parameters.get(key);
        }
    
        private void test() {
            parameters.put("myint", 10);
            parameters.put("lol", "go fuck yourself");
    
            int id = this.<Integer>getParameter("myint");
            assert id == 10;
    
            String txt = getParameter("lol");
            assert txt == "go fuck yourself";
        }
    
        public static void main(String[] argv) {
            Mock mock = new Mock();
            mock.test();
        }
    }
    Reply With Quote  
     

  7. Thankful user:

    K2

  8. #7  
    Ich bin ein Sicherheitsexperte
    K2's Avatar
    Join Date
    Oct 2010
    Posts
    802
    Thanks given
    801
    Thanks received
    193
    Rep Power
    403
    Quote Originally Posted by super_ View Post
    taking my advice on using a markup for protocol definitions eh?

    if i understand your question correctly, then packet.getParameters() is an instance of JSONObject... why wouldn't parameters.getInt("myint") work?
    in the case that you want to maintain some constant interface for all config, you could just do...
    Code:
    int id = this.<Integer>getParameter("myid");
    to demonstrate:
    Code:
    import java.util.Map;
    import java.util.HashMap;
    
    class Mock {
    
        private Map parameters = new HashMap();
    
        private <E> E getParameter(String key) {
            return (E) parameters.get(key);
        }
    
        private void test() {
            parameters.put("myint", 10);
            parameters.put("lol", "go fuck yourself");
    
            int id = this.<Integer>getParameter("myint");
            assert id == 10;
    
            String txt = getParameter("lol");
            assert txt == "go fuck yourself";
        }
    
        public static void main(String[] argv) {
            Mock mock = new Mock();
            mock.test();
        }
    }
    Can I fuck you tysm
    Respectfully,

    Steve Kreitzer
    Engineer, Blackspoke
    [email protected]
    "Those who care will always make a way."
    Reply With Quote  
     

  9. #8  
    Renown Programmer
    veer's Avatar
    Join Date
    Nov 2007
    Posts
    3,746
    Thanks given
    354
    Thanks received
    1,370
    Rep Power
    3032
    so i take it i answered your question?
    Reply With Quote  
     

  10. Thankful user:

    K2


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Replies: 15
    Last Post: 05-18-2012, 09:21 PM
  2. JSON Cache
    By Laxika in forum Website Development
    Replies: 4
    Last Post: 03-06-2012, 01:23 PM
  3. [Java] O'Reilly - Java NIO [Java]
    By Sir Zap in forum Application Development
    Replies: 5
    Last Post: 02-25-2011, 09:26 AM
  4. Replies: 14
    Last Post: 11-01-2010, 11:29 PM
  5. [Proccessing] [Java RPG] Scripting lang based from java
    By CTucker in forum Application Development
    Replies: 11
    Last Post: 04-04-2010, 02:25 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •