how can i split the particular data from a string? [duplicate] - android

This question already has answers here:
Split string at commas
(4 answers)
Closed 7 years ago.
I am getting the lat-long from the current location now how can i split the only lat long values from a given string :-
"lat/lng: (23.0326542,72.5279697)"? i want to get only "23.0326542,72.5279697".

Try this:
String line = "lat/lng: (23.0326542,72.5279697)";
String[] array = line.split(",");
for (int i = 0; i < array.length; i++) {
Pattern pattern = Pattern.compile("(\\d+\\.\\d+)|(\\d+)");
Matcher m = pattern.matcher(array[i]);
if (m.find()) {
System.out.println(m.group(1));
}
}

It looks like you should be taking the substring of this string, from the beginning of the value you need, to the length of the string.

Related

How to get variable from JSON in android studio [duplicate]

This question already has answers here:
How do I parse JSON in Android? [duplicate]
(3 answers)
Closed 4 years ago.
I am trying to get a single string from a JSON response.("oppdatert")
The following line is returned when i run my function:
{"isError":false,"errorMsg":null,"response":{"oppdatert":"ja"}}
This is an async-task, and under onTaskComplete i have the following code:
String approved = null;
JSONArray myJsonArray = JSONreturn.getJSONArray("response");
myList.clear();
for(int i = 0; i < myJsonArray.length(); i++) {
JSONObject brukerOppdatert = myJsonArray.getJSONObject(i);
approved = brukerOppdatert.getString("oppdatert");
The application crashes after "JSONArray = myJsonArray = ..."
This method have worked on other json-array, but the difference is that in the past there have been more then one object.
any ideas?
First of all your response is JSONObject not JSONArray. so
JSONObject myJsonobject = JSONreturn.getJSONObject("response");
myList.clear();
myJsonobject.getString("oppdatert");
So
myJsonobject.getString("oppdatert")
this will get you, your required string.

Android json array: how do I get and specific string? [duplicate]

This question already has answers here:
How do I parse JSON in Android? [duplicate]
(3 answers)
Closed 6 years ago.
I have a Json script which get data from a Mysql database and print it in different TextViews. The thing I want to know is how can I take for example only the "idE" value and print it in the "TextViewTitulo" TextView
try {
String response = "[{"0":"1","id":"1","1":"f3n","idE":"f3n","2":"bar","tipo":"bar"},{"0":"1","id":"1","1":"f3n","idE":"f3n","2":"bar2","tipo":"bar2"}]"
JSONArray array;
array = new JSONArray(response);
StringBuilder sb = new StringBuilder();
for (i=0; i<array.length(); i++) {
// chain each string, separated with a new line
sb.append(array.getString(i) + "\n");
}
// display the content on textview
textViewTitulo.setText(sb.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
My code works fine, but is displays the whole string of values
You have 2 objects in your JSONArray, and 1 textview to show the data in, so I'm adding append " " to separate them.
for (i=0; i<array.length(); i++) {
sb.append(array.getJSONObject(i).getString("idE"));
sb.append(" ");
//do whatever with it
}
textViewTitulo.setText(sb.toString());
Of course you can get it, you just have to iterate through the whole JSONArray. If you know the value you are looking for, you have to search for it, if you know the index - it is easier, you just have to get it. So for the first case, you could do something like:
for (int i = 0; i < array.length(); i++) {
JSONObject current = array.getJSONObject(i);
// In your case the JSONObject is more complicated
// so you need to iterate over it too
Iterator<?> keys = current.keys();
while(keys.hasNext()) {
String key = (String)keys.next();
String value = current.get(key);
// Do something with the value
}
// Or If you know what key you are looking for, you may do
String value = current.get("idE");
// Do something with value
}

How do I split these String Arraylist [{ } , {}, { }...] to strings or Integer? [duplicate]

This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 8 years ago.
I have,
[{"GroupPosition":0,"ChildPosition":0},{"GroupPosition":0,"ChildPosition":1},{"GroupPosition":1,"ChildPosition":0}]
How do I split them into:
GroupPosition:
[0,0,1] or {0,0,1}
ChildPosition:
[0,0,0] or {0,0,0}
Your input is in json format. Consider my code is sample.
private static ArrayList<String> array_1 = new ArrayList<String>();;
String json = [{"GroupPosition":0,"ChildPosition":0},{"GroupPosition":0,"ChildPosition":1},{"GroupPosition":1,"ChildPosition":0}];
JSONArray leagues = new JSONArray(json);
for (int i = 0; i < leagues.length(); i++) {
JSONObject obj = leagues.getJSONObject(i);
array_1.add(obj.getString("GroupPosition"));
}

Convert a string to int array [duplicate]

This question already has answers here:
How to convert object array to string array in Java
(11 answers)
Closed 9 years ago.
I need to convert a string stored like,
textView.setText(myVar);
Here
myVar = "23,45,64,78";
I would like it to convert into array like below,
int[] xAxis = new int[]{23,45,64,78};
How can I achieve this? Thanks
Try this:
String arr = "[23,45,64,78]";
String[] items = arr.replaceAll("\\[", "").replaceAll("\\]", "").split(",");
int[] results = new int[items.length];
for (int i = 0; i < items.length; i++) {
try {
results[i] = Integer.parseInt(items[i]);
} catch (NumberFormatException nfe) {};
}

How to show it point after two digit in java?

I am Android developer.I get from web service result 45.0000.i am stored ArrayList.But i want show it point after two digit only.
Example output 45.00 only .Please give me solution.
Use DecimalFormat class to achieve this.
DecimalFormat df=new DecimalFormat("##.##");
String formate = df.format(value);
double finalValue = (Double)df.parse(formate) ;
you can use String.format, it returns a formatted string using the specified format string and arguments
String.format("$%.2f", value);
also String.format uses your Locale
int index = 0;
for (; index < yourList.size (); index++) {
String tmpvalue = yourList.get(index);
String conValue = String.format("$%.2f", Float.parse(tmpvalue));
yourList.set(index, conValue);
index++;
}

Categories

Resources