Results 1 to 13 of 13

Thread: Java on Android Decoding a string

  1. #1

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Java on Android Decoding a string

    I'm calling IIS web methods from a java app on an Android device.

    The IIS web method returns a JSON string that looks like this:

    Code:
    {"d":"{\"login\":true,\"success\":\"\",\"message\":\"\u003cbr /\u003eLog in failure!\"}"}
    I'm trying to decode it - get rid of the \'s in front on the double-quotes - and turn those \u003c and e's into <> brackets.

    I am using this code

    Code:
    json2 = URLDecoder.decode(result, "UTF-8");
    and it's doing nothing to the string.

    So I'm guessing this is NOT URL encoded - it's some other kind of encoding.

    Any help greatly appreciated - thanks!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,901

    Re: Java on Android Decoding a string

    It looks like an "escaped" string, not a normal UTF8 string, though the UTF8 are also escaped

    http://stackoverflow.com/questions/3...trings-in-json

  3. #3

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Java on Android Decoding a string

    Thanks for that link. It doesn't seem to have any specific JAVA method to un-escape the string - but it does have some info that will help me search further...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,901

    Re: Java on Android Decoding a string

    It looks to me as if only the " character needs to be escaped. All other encoding methods are free to the system/programmer/user/client etc etc.
    I can't find any official reference for JSON string encoding, but the JSON website itself comes close.
    http://www.json.org/

  5. #5
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Re: Java on Android Decoding a string

    When it is successfully decoded what would be the output?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  6. #6
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Re: Java on Android Decoding a string

    Is this the output you are looking for Sir? Just tried your string and used NetBeans for now but I'm sure the library should work with Eclipse also.
    Name:  json.jpg
Views: 536
Size:  24.1 KB
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  7. #7

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Java on Android Decoding a string

    @dee-u - yes, that is exactly what I was hoping for. NetBeans?? Is that an IDE you are using?

    What library are you using for the JSON work? The coder I have here has tried a couple and none did that proper type of decoding.

    And you can call me "Steve" - we've worked together before

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Re: Java on Android Decoding a string

    The library that I used in the json-simple, it was one of the first to come up on my google. I initially thought it did not work since I did not know how json works but realized later that I just need to get the "d" object out of that json string. Here's the code:

    Code:
    import org.json.simple.JSONArray;
    import org.json.simple.JSONObject;
    import org.json.simple.parser.JSONParser;
    import org.json.simple.parser.ParseException;
    
    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            
            String s = jTextField6.getText();
            JSONParser parser=new JSONParser();
    
            System.out.println("=======decode=======");
    
            try 
            {
                Object obj=parser.parse(s);
                            
                JSONObject jsonObject =  (JSONObject) obj;
    
                String name = (String) jsonObject.get("d");
                System.out.println(name);
            }
            catch(ParseException pe){
                System.out.println("position: " + pe.getPosition());
                System.out.println(pe);
            }
    
        }
    As you may notice I am inputting the json string in a textfield and retrieving it from there. And yes, NetBeans is the IDE we use, I will also try Eclipse since my goal is to learn Android programming and it seems Eclipse is the IDE for it.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  9. #9
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Re: Java on Android Decoding a string

    Just to make sure, I did install Eclipse and 'imported' the test project I tried earlier with NetBeans and I got the same result.
    Name:  Eclipse.jpg
Views: 1270
Size:  35.4 KB
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  10. #10
    Addicted Member
    Join Date
    Oct 2008
    Location
    Califorina
    Posts
    235

    Re: Java on Android Decoding a string

    I use jackson https://github.com/FasterXML/jackson-core I've never tried it for Android dev though.

  11. #11

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Java on Android Decoding a string

    And that does JSON? The link name indicates XML.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  12. #12
    Addicted Member
    Join Date
    Oct 2008
    Location
    Califorina
    Posts
    235

    Re: Java on Android Decoding a string

    Yeah, I thought that too when first looked at the page. But yes, it does parse JSON strings. http://wiki.fasterxml.com/JacksonInFiveMinutes

  13. #13
    Addicted Member
    Join Date
    Oct 2008
    Location
    Califorina
    Posts
    235

    Re: Java on Android Decoding a string

    Here is my little test I used when I first started messing with Jackson

    my class for the json data structure.
    Code:
    import java.math.BigDecimal;
    import java.util.List;
    import com.fasterxml.jackson.annotation.JsonProperty;
    
    public class CampaignParser {
    	
    	//Defines the Campsegment of the campaign json string
    	public static class Campsegments {
    		@JsonProperty("segmentId") private int segmentid;
    		@JsonProperty("countDesired") private int countDesired;
    		@JsonProperty("countAvailable") private int countAvailable;
    
    		public int GetSegmentid() {
    			return segmentid;
    		}
    
    		public int GetCountAvailable() {
    			return countAvailable;
    		}
    
    		public int GetCountDesired() {
    			return countDesired;
    		}
    
    		public void SetSegmentid(int segmentid) {
    			this.segmentid = segmentid;
    		}
    
    		public void SetCountDesired(int countDesired) {
    			this.countDesired = countDesired;
    		}
    
    		public void SetCountAvailable(int countAvailable) {
    			this.countAvailable = countAvailable;
    		}
    		
    
    	} //end Campsegments
    	
    	//defines HotSpots 
    	public static class HotSpots {
    		@JsonProperty("opportunityId") private List<String> opportunityId;
    		@JsonProperty("longitude") private BigDecimal longitude;
    		@JsonProperty("latitude") private BigDecimal lat;
    		@JsonProperty("countDesired") private int countDesired;
    		@JsonProperty("countAvailable") private int countAvailable;		
    
    		public List<String> getOpportunityId() {
    			return opportunityId;
    		}
    		public void setOpportunityId(List<String> opportunityId) {
    			this.opportunityId = opportunityId;
    		}
    		public BigDecimal getLongitude() {
    			return longitude;
    		}
    		public void setLongitude(BigDecimal longitude) {
    			this.longitude = longitude;
    		}
    		public BigDecimal getLat() {
    			return lat;
    		}
    		public void setLat(BigDecimal lat) {
    			this.lat = lat;
    		}
    		public int getCountDesired() {
    			return countDesired;
    		}
    		public void setCountDesired(int countDesired) {
    			this.countDesired = countDesired;
    		}
    		public int getCountAvailable() {
    			return countAvailable;
    		}
    		public void setCountAvailable(int countAvailable) {
    			this.countAvailable = countAvailable;
    		}
    	} //HotSpots
    
    	public static class Segments {
    		@JsonProperty("segments") private String segment;
    		
    		public void SetSegments (String segs) {
    			this.segment = segs;
    		}
    		public String GetSegments() {
    			return this.segment;
    		}
    
    	} //Segments
    
    	private String account;
    	private String routes;
    	@JsonProperty("segments") private List<String> seg;
    	@JsonProperty("campsegments") private List<Campsegments> campSegments;
    	@JsonProperty("hotspots") private List<HotSpots> hotSpots;
    	private double radius;
    	private String includePhones;
    	private int phonesCount;
    	private BigDecimal orderTotal;
    		
    	public List<Campsegments> GetCampsegments() {
    		return campSegments;
    	}
    
    	public List<HotSpots> GetHotSpots() {
    		return hotSpots;
    	}
    	
    	public void SetCampsegments(List<Campsegments> c) {
    		this.campSegments = c;
    	}
    
    	public String GetAcct() {
    		return this.account;
    	}
    
    	public void SetAcct(String acct) {
    		this.account = acct;
    	}
    
    	public String GetRoutes() {
    		return this.routes;
    	}
    
    	public void SetRoutes(String route) {
    		this.routes = route;
    	}
    	
    	public void SetRadius(double radius) {
    		this.radius = radius;
    	}
    	
    	public double GetRadius() {
    		return this.radius;
    	}
    	
    	public void SetIncludePhones(String phones) {
    		this.includePhones = phones;
    	}
    	
    	public String GetIncluePhones() {
    		return this.includePhones;
    	}
    	
    	public void SetOrderTotal(BigDecimal total) {
    		this.orderTotal = total;
    	}
    	
    	public BigDecimal GetOrderTotal() {
    		return this.orderTotal;
    	}
    	
    	public void SetPhonesCount (int phonesCount) {
    		this.phonesCount = phonesCount;
    	}
    	
    	public int GetPhonesCount() {
    		return this.phonesCount;
    	}	
    	
    	public void SetSegments (List<String> segs) {
    		this.seg = segs;
    	}
    	
    	public List<String> GetSegments() {
    		return this.seg;
    	}
    
    }
    my test file
    Code:
    {"account":"881gqyerfXj5satv","segments":"2, 25, 9, 23, 8, 28, 7, 17, 15, 19","hotspots":[{"opportunityId":1,"longitude":"-122.24345700000003","latitude":"40.19201","countDesired":92,"countAvailable":0},{"opportunityId":2,"longitude":"-122.233901","latitude":"40.1678","countDesired":44,"countAvailable":0}],"radius":"0.5","includePhones":"Yes","phonesCount":7,"orderTotal":"129.90"}

    the quick test
    Code:
    import java.io.File;
    import java.io.IOException;
    import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
    import com.fasterxml.jackson.annotation.PropertyAccessor;
    import com.fasterxml.jackson.core.JsonParseException;
    import com.fasterxml.jackson.databind.*;
    
    public class testJackson {
    
    	public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException{
    		
    		ObjectMapper mapper = new ObjectMapper();
    		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    		mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
    		mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    		mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
    
    		CampaignParser model = mapper.readValue(new File("c:\\temp\\test.json"), CampaignParser.class);
    
    	        System.out.println(mapper.writeValueAsString(model)); //displays the json string
    	        System.out.println(model.GetIncluePhones()); // extracts IncludePhones value
    	        System.out.println(model.GetPhonesCount()); // extract Phone counts.
    	    
    	} 
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width