0

I am trying to extract the "first_name" and "last_name" from a long string of information that look like this:

{
    "123123123": {
        "id": "12321312****",
        "email": "***************",
        "first_name": "Marcus",
        "gender": "male",
        "last_name": "Bengtsson",
        "link": "https://www.facebook.com/app_scoped_user_id/123123123/",
        "locale": "en_EN",
        "middle_name": "Peter",
        "name": "Marcus Peter Bengtsson"
    }
}

The way I do it (which is probably horribly wrong and a pretty bad solution) is that I firstly extract the substring from "first_name" to "link" with this code:

String subStr = str.substring(str.indexOf("first_name"), str.lastIndexOf("link"));

Then I get:

first_name":"Marcus","gender":"male","last_name":"Bengtsson","

Then I do the same thing but from ":" to "gender" to get the "first_name:

String firstNameOfUser = subStr.substring(subStr.indexOf(":")+2, subStr.lastIndexOf("gender")-3);

Then the same thing for "last_name":

String lastNameOfUser = subStr.substring(subStr.indexOf(""last_name"")+12, subStr.lastIndexOf(",")-1);

And lastly I append the both strings with a space in between:

String nameOfUser = new StringBuilder().append(firstNameOfUser).append(" ").append(lastNameOfUser).toString();

And then I get:

Marcus Bengtsson

There is probably a much better way to do this but I can't seam to figure out how.

Jonathan
  • 20,053
  • 6
  • 63
  • 70

4 Answers4

8

This looks like a JSON so it would be much better to parse it as such using one of many available parsers and then extract the data.

Crozin
  • 43,890
  • 13
  • 88
  • 135
  • Okay, but the data I'm fetching is from a webbpage. How do I save it down first? Im using InputStream and BufferedReader to get the data from the site and then I patch it up to a String with StringBuilder. This seams like a whole different question now. – user3191617 May 26 '15 at 09:22
  • Most libraries are capable of using `InputStream` directly. You won't even need to build a string for them. – Crozin May 26 '15 at 10:02
  • Well I tried the Json thing but now I am not able to save the fields from the JSONObject. I get the error `org.json.JSONException: JSONObject["first_name"] not found.` But when I first print out the whole JSONObject I clearly see all the fields. No clue of what I am doing wrong... Oh never mind, I got it now. – user3191617 May 26 '15 at 10:35
0

The given String is a JSON, use the JSONParser to parse it to json and then extract the data needed.

kondu
  • 410
  • 3
  • 11
0

thats a json? visit this link JSON Example

nameOfUser = 123123123.first_name + " " + 123123123.last_name;

i hope this will help you

-1

As @Crozin wrote it looks like json but if you can't parse it in this way you can always use regex. Just use Matcher, Pattern with a regex ^.*\"first_name\":\"([a-zA-Z]+)\".*\"last_name\":\"([a-zA-Z]+)\".*$.

pepuch
  • 6,346
  • 7
  • 51
  • 84
  • I'd say you can't _always_ use regex. If the data is simple enough and you know what will be contained then it might work but since JSON can get quite complex regex will eventually break. – Thomas May 26 '15 at 07:46
  • Downvote: it is a very poor, error prone solution. Regular expressions might look like a quick'n'easy solution but most of the time when you're dealing with complex, structured language (such as JSON) they're not. – Crozin May 26 '15 at 07:47
  • As I wrote regex can be used if it can't be parsed as json. – pepuch May 26 '15 at 07:47