This is going to be a pretty ridiculous question so brace yourselves.
I need to build an URL string which will update some status variables in a website.
Basically, since the structure of the URL must be in JSON's structure, I'm having a lot of trouble getting variables in the middle of the string.
"http://mywebsite.com/index.php?data={\"Number\":\"1234567890001\",\"TS\":\"15/11/15%2008:08:31:44\",\"SER\":\"53543D303B44723D4E616F\"}"
Basically, I need to replace "1234567890001\" for the variable numberX and "53543D303B44723D4E616F\" for the variable serX.
Can someone lend me a hand?
I've tried everything with +'s & company but I'm not being able to pull it up.
Thanks!
Firstly, It's hard to understand why you are not using GET parameter. It will be a lot more easy to get all parameters
However, if you want to stay with this approach, you can simply get your data value and make a replace, like : replace("\\","")
This will provide a String similar to this : {"Number":"1234567890001","TS":"15/11/15%2008:08:31:44","SER":"53543D303B44723D4E616F"}
And then, you can simply parse it using JSON Library like GSON.
Related
Im trying to pass an array of strings from a java servlet to a jsp file.
now, i want to use that array in a javascript function in that same jsp file.
is this possible?
You'd have to first serialize it (JSON format is probably easiest) and then deserialize it in javascript to be able to use it as an array.
Yes, you'll need to convert the array to JSON first though, then have your JavaScript function parse that JSON. At that point it can work with it as a standard JS array (because at that point, it is).
EDIT - I did not mean to imply that JSON is absolutely required, nor that it is the only possible solution. It's simply an option, and in my opinion probably the best and easiest for what you're trying to accomplish.
I have a RuneScape Private Server project coded in Java, and am trying to code a personal "tag" that players can use. I have managed to do this, but everytime there is a restart on the server, their "tag" gets reset to "null".
Their "tag" is initalized by doing a command ";;settag [name]". Their tag is then set to whatever they want. I have done this through a string:
if (command[0].equals("settag")) {
newTag = getCompleteString(command, 1);
newTag = player.yellTag
player.sendMessage("Your tag is now:" +newTag);
}
I am unsure what the most efficient way to fix this would be, I am thinking of just loading and saving through .xml/.txt files. By the way, player.yellTag is where the next command (::mytag) searches it from, which works fine, until there is a restart of the server.
it all depends on the context of your application. If you are planning on having less than a few hundreds players, then a xml file may be ok. You should look at JAXB, which is, afaict, the standard way to store your objects in Java. You can also store them as JSON files, using gson which is way simpler to use and implement than XML stuff.
But if you get to have more than thousands of players, you may want to get some more efficient way to serialize your tags by putting them in a database, and thus an ORM library like hibernate could help you do that.
You may want to make your own stuff, like a tag directory full of files named after unique ids of your players containing the players' tag... It's a lot more "hackish" but still quite efficient.
I am writing a RESTful web service using Java which should be able to take as (POST) input an unknown number of parameter name, value pairs. I am thinking what is the best way to achieve this.
The POST input will be JSON with the expected format being:
{"input" :
{ "x" = 1, "y"="Hello, "z"="1.2.3.4",... }
}
The parameter values are expected to be a mix of int/long/float/string etc. The expected behavior is to save this input to the DB and provide it back on request. The tricky part is with the parameter names being unknown, I cannot write getter and setter methods. Is there a simple way for handling this?
Any pointers in the right direction will be very much appreciated. Can I read this as a List? Do I need to write Custom MessageBodyReaders? Is there some other way of achieving this? The only option I can think of is to have these sent as a file and parse it but am hoping that there are simpler/cleaner solutions. Thank you for any pointers...
If all you need to do is save it and make it available on request, why parse it at all? Simply write the json as a string to your database, and return it as a string while setting the response type to "application/json". The one caveat being you're going to have to have some way to identify which json string you want and associate a unique id that you're going to be able to determine when you're requesting the specific json string in the future.
Something worth noting here would be that when you don't parse something, you don't actually know what it is. Even though a lot of modern libraries/frameworks sanitize against sql injection/xss already, it would be worth it to verify that this is being done in your server if you don't already know for sure.
That being said, if you're still feeling as though you need to parse the json, I would look into gson: http://code.google.com/p/google-gson/
Can someone please help me create a method to get the integers (listed below) from this link and store them in an array using Java? I'm new to Java and I've searched around and I can't any info or working examples of how to do it.
The integers are:
17,04,2011,1,2,7,10,13,23,24,25,26,27,38,39,41,43,45,48,49,55,59,62
All integers will change each time the method is called.
You need to do this:
retrieve the web page. Use URL and URLConnection for this.
Parse the HTML. It looks like this is an XHTML page, so you could use any XML API for this. As an alternative, for this simple structure something like a regular expression that throws away anything which is in <...> could work, too.
put the numbers from the HTML into a new array. You might want to count them first, to know the right size, or put them in while you are parsing, and later cut off the array. (Or they are always the same number.)
Does anyone know if there's an easy way to implement this:
I have an array of strings (~650 elements) and would like to google each element, and then have the number of results acquired in each search stored in another integer array. I looked at the Google API, but it seems a little daunting to me, it seems like a fairly simple task, but can't seem to find any guidance on how to do this.
Help is much appreciated.
Thanks.
My first thought was to do the following. I don't think its the fastest or most elegant way, but I think it should do the trick:
If you check a URL on Google, they all look the same:
http://www.google.com/search?q='here your searchword'
So i would just iterate over your array and for each string create a url like e.g.
http://www.google.com/search?q=test
do a HTTP-Post for that and parse the result.
Here is a short example of the HTTPPost. You can also see how to get the response. I don't know how googles response is looking, but if you just print out the whole response, I think you will see how you have to parse it.