-4

I'm passing a String parameter into a java function I wrote.

When I pass the string parameter the method is accepting my parameter as follows
http://mywebsite.com/getCity.php?StateID={"state":"Alabama"}

I want my method to accept my string parameter as follows http://mywebsite.com/getCity.php?StateID=Alabama

How do I get rid of {"state":"Alabama"}?

Hey Guys,

I'm building an android app. My syntax below is definitely java. I'm going to show you where I'm having trouble. I'm having trouble on the first line of the method so to show you the entire method would be silly.

    public JSONArray getDetails(String StateID) {

        // this Log.e is showing {"state":"Alabama"}
        // how do I get it to show Alabama?
        Log.e("StateID= " + " = ", StateID); 
}
bobhope
  • 113
  • 1
  • 3
  • 10

2 Answers2

0

I'm don't know Java, but the best method would be to deserialize the string into a Java object that had a state property. See: http://www.javacreed.com/gson-deserialiser-example/

Alternatively, you could just do something ugly like this:

StateID.substring(10, StateID.length - 2)

Which should trim {"state":" off the front, and trim "} off of the back.

Ray Suelzer
  • 4,026
  • 5
  • 39
  • 55
0

You've got a JSON object there (and it really should be URL encoded if it's going to live on a URL like that).

Treat it like a JSON object and use JSONObject to decode it. Deal with the checked exception thrown however you see fit.

JSONObject jsonObject = new JSONObject(StateID);
System.out.println(jsonObject.getString("state"));
Makoto
  • 104,088
  • 27
  • 192
  • 230