Extracting only a specified part of a string in Android - android

Let's say I have a string :
{"id":"123","xCoord":"01.234567","yCoord":"89.012345","etc.":"etcetc"}
I want to extract only the xCoord part - the number 01.234567 and put it into a string array String[] xCoords = {};
I cannot use the public String substring (int start, int end) function because in future the id will eventually grow up and I don't have a firm index to use.
What would you suggest me - is there any way of extracting only the symbols after "xCoord":" and before ","y...

The best (and most reliable) option would be to convert your string (which is valid JSON) into an object and reference it that way.
Convert JSON String to Java Object or HashMap

Related

Gson converting int into double when parsing json string

I have this JSON file into my assets. I am parsing it using Gson into the following model class:
public class SearchRequest {
private ArrayList<String> _source;
private int from;
private int size;
private Object sort;
private Object query;
public void setFrom(int from) {
this.from = from;
}
public void setSize(int size) {
this.size = size;
}
public void setArtist(String artistName) {
Gson gson = new Gson();
JsonObject object = gson.toJsonTree(query).getAsJsonObject();
JsonArray should = object.getAsJsonObject("function_score").getAsJsonObject("query")
.getAsJsonObject("bool").getAsJsonArray("should");
should.get(0).getAsJsonObject().getAsJsonObject("match").addProperty("album_artist", artistName);
should.get(1).getAsJsonObject().getAsJsonObject("nested").getAsJsonObject("query")
.getAsJsonObject("bool").getAsJsonArray("must").get(0).getAsJsonObject()
.getAsJsonObject("match").addProperty("tracks.artist", artistName);
query = gson.fromJson(object.toString(), query.getClass());
}
}
When I convert this JSON into an object of this class, the query object becomes a LinkedTreeMap. But in this conversion, the key offset which is an integer, becomes double. In my JSON (line number 50), offset is 0, but after conversion, its 0.0. Screenshot:
Why is this happening? How to fix this?
How to fix this?
It's not a subject to be fixed and nothing to be worried about.
Why is this happening?
Your JSON<->Java mapping does not provide any mappings except the top-most one. That makes Gson work like that due to lack of the target type information, and LinkedTreeMap is used internally. Literals like 0 that may look like integers are also legal floating point values from the JSON format point of view: the JSON specification declares numbers only, and does not make any corrections on "integerness". Having no enough information on deserializing the data types, Gson applies the default parsing policies, and chooses java.lang.Double as a type that can hold any other standard numeric values that can hold less significance bits (longs can fit the room of doubles easily; but not sure what Gson does for BigDecimals -- JSON specifications does not seem to make any limits). So this is just internal Gson representation and you have a few options on that:
You can declare a mapping (write or generate by a specialized tool) that would even avoid internal LinkedTreeMaps. Tedious? Maybe. But much more power in type safety, javac control, or your favorite IDE navigation and suggestions.
Gson provides a bunch of methods to convert a JSON tree value to a target type: getAsJsonObject(), getAsJsonArray(), getAsInt(), and more allowing to get the target object in the representation you want. For example,
final Object value = searchRequest.query
.get("function_score").getAsJsonObject()
.get("functions").getAsJsonArray()
.get(0).getAsJsonObject()
.get("linear").getAsJsonObject()
.get("date").getAsJsonObject()
.get("offset").getAsInt();
System.out.println(value + " " + value.getClass());
gives:
0 class java.lang.Integer
because of get("offset").getAsInt() that's internally implemented as return isNumber() ? getAsNumber().intValue() : ...; for JSON primitives.
You can apply partial mappings. For example, you can extract the date JSON tree and convert it to a special mapping having the private int offset; field declared: gson.fromJson(dateJsonObject, DateMapping.class).
Again, Gson just does not have enough type information and works really fine.

How can i get few characters from String?

I want to retrieve few characters from string i.e., String data on the basis of first colon (:) used in string . The String data possibilities are,
String data = "smsto:....."
String data = "MECARD:....."
String data = "geo:....."
String data = "tel:....."
String data = "MATMSG:....."
I want to make a generic String lets say,
String type = "characters up to first colon"
So i do not have to create String type for every possibility and i can call intents according to the type
It looks like you want the scheme of a uri. You can use Uri.parse(data).getScheme(). This will return smsto, MECARD, geo, tel etc...
Check out the Developers site: http://developer.android.com/reference/android/net/Uri.html#getScheme()
Note: #Alessandro's method is probably more efficient. I just got that one off the top of my head.
You can use this to get characters up to first ':':
String[] parts = data.split(":");
String beforeColon = parts[0];
// do whatever with beforeColon
But I don't see what your purpose is, which would help giving you a better solution.
You should use the method indexOf - with that you can get the index of a certain char. Then you retrieve the substring starting from that index. For example:
int index = string.indexOf(':');
String substring = string.substring(index + 1);

Not able to get name/value pairs from JSON object, when using variable

Not able to get name/value pairs from JSON object, when using the variable but able to read it when hard coding the name.
To better explain :
1) My JSON object is like this -
{.....
{ "rates":{ "name1": value1, "name2": value2 ...etc }
...}
2) I am able to read this object in my android app.
3) Now this rate object name value pairs, i am trying to read based on user input -
String s1 = '"'+name1+'"'; // here name1 i got from user input, & converted into string
4) Now when i am trying to get the value from rates object, i am getting null exception -
JSONObject rateObject = jObject.getJSONObject("rates"); //able to get
complete object
String rate1 = (String) rateObject.get(s1); // giving NULL exception
5) But if i use hard code string, it works -
String rate1 = (String) rateObject.get("name1"); // working
Any pointers why its not working while using variable.
thanks
Thanks for suggestions, i sorted out the problem. There are 2 mistakes i was doing - 1) Using the quotes as correctly pointed out by others and 2) casting the double value to string. Correcting both has resolved my problem :)
In terms of your final code snippet, you are actually doing
String rate1 = (String) rateObject.get("\"name1\""); //note the extra quotes
because you have bookended the user input string with double-quote characters. You just want the input string itself with no bookending. The quotes in the JSON notation serve to delineate each key name; the quotes are not part of the key name itself.
You need to omit the quotes when you create s1:
String s1 = name1;
Or, if name1 is not a String already:
String s1 = name1.toString();
Replace:
String s1 = '"'+name1+'"';
with:
String s1 = name1;

Extract text from String

I have one String and into this string I have a url between two characters # such as "Hello world #http://thisurl# my name is Pippo" I want to take the url (http://thisurl) between two #.
How can I do ? Thanks
String data[] = str.split("#"); //spilliting string and taking into array
ArrayList<String> urlList = new ArrayList<String>();
for (int i = 0; i < data.length; i++) {
if(data[i].contains("http://"))
urlList.add(data[i]); //if string contains "http://" it means it is url save int list.
}
now you can get all uls from urlList.get(i) method.
this urlList will give you all the urls available in the string. I dint applied any null or other check. Apply it and try. If want something else try modifying content and checks.
Try String.split(). You really should be trying to google these things first.
here is an example - http://www.java-examples.com/java-string-split-example
The split method divides a string into several strings and store them into an array using a delimiter which can be defined by you.
the second element in the resulting array will be your URL

Android String Array Manipulation

I have a lengthy string in my Android program.
What I need is, I need to split each word of that string and copy that each word to a new String Array.
For eg: If the string is "I did android program" and the string array is named my_array then each index should contain values like:
my_array[0] = I
my_array[1] = did
my_array[2] = Android
my_array[3] = Program
A part of program which I did looks like this:
StringTokenizer st = new StringTokenizer(result,"|");
Toast.makeText(appointment.this, st.nextToken(), Toast.LENGTH_SHORT).show();
while(st.hasMoreTokens())
{
String n = (String)st.nextToken();
services1[i] = n;
Toast.makeText(appointment.this, st.nextToken(), Toast.LENGTH_SHORT).show();
}
Can any one please suggest some ideas..
Why not use String.split() ?
You can simply do
String[] my_array = myStr.split("\\s+");
Since '|' is a special character in regular expression, we need to escape it.
for(String token : result.split("\\|"))
{
Toast.makeText(appointment.this, token, Toast.LENGTH_SHORT).show();
}
You can use String.split or Android's TextUtils.split if you need to return [] when the string to split is empty.
From the StringTokenizer API docs:
StringTokenizer is a legacy class that
is retained for compatibility reasons
although its use is discouraged in new
code. It is recommended that anyone
seeking this functionality use the
split method of String or the
java.util.regex package instead.
Since String is a final class, it is by default immutable, which means you cannot make changes to your strings. If you try, a new object will be created, not the same object modified. Therefore if you know in advance that you are going to need to manipulate a String, it is wise to start with a StringBuilder class. There is also StringBuffer for handling threads. Within StringBuilder there are methods like substring():
substring(int start)
Returns a new String that contains a subsequence of characters currently contained in this character sequence.
or getChars():
getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
Characters are copied from this sequence into the destination character array dst.
or delete():
delete(int start, int end)
Removes the characters in a substring of this sequence.
Then if you really need it to be a String in the end, use the String constructor(s)
String(StringBuilder builder)
Allocates a new string that contains the sequence of characters currently contained in the string builder argument.
or
String(StringBuffer buffer)
Allocates a new string that contains the sequence of characters currently contained in the string buffer argument.
Although to understand when to use String methods and when to use StringBuilder, this link or this might help. (StringBuilder comes in handy with saving on memory).

Categories

Resources