0

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...

Stefan Doychev
  • 711
  • 3
  • 12
  • 30

1 Answers1

2

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

Community
  • 1
  • 1
jondavidjohn
  • 61,812
  • 21
  • 118
  • 158
  • The string I have is already a response from a database on a server. Now I put a `for (int i=0; i – Stefan Doychev Aug 03 '11 at 15:27
  • Narrow down the line of code which is throwing the error. It could be coming from getJSONObject or from xCoords[i] = xCoordReturned - I would guess the latter. Either make sure that your xCoords array is the same length as jArray.length prior to the loop, or turn xCoords into an ArrayList and use xCoords.add(). – Josh Aug 03 '11 at 16:58
  • I've posted this as a separate question - I got the part of the string I was looking for with the `String xCoord = json_data.getString("xCoord");` - thanks for the answer. – Stefan Doychev Aug 04 '11 at 08:29