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

1

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

The string I have is already a response from a database on a server. Now I put a for (int i=0; i<jArray.length(); i++) { JSONObject json_data = jArray.getJSONObject(i); String xCoordReturned = json_data.getString("xCoord"); xCoords[i] = xCoordReturned; But I receive the ArrayOutOfBounds error - is this the way of getting the string from the JSON object or this is the reason of the error?
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().
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.