0

Hi guys I'm newbie in android dev.. can you help me with this one? I am trying to get the filename in string. I know that I need to substring it but I failed to get the right output. For example I have the string value of:

{"Filename":"23476-love-823873.jpg","ChunkId":1,"ChunkLength":201929,"FileLength":12122}

and I want to get only the filename which is "23476-love-823873.jpg" How can I do that? Thanks for helping.

dolphinately
  • 69
  • 1
  • 2
  • 13

6 Answers6

1

You've got structured data there, so you shouldn't do this blindly with a substring. Instead, you can parse your string as JSON and then access the appropriate property of the new object. There's an Android JSON library that you can import from org.json. Specifically, you'll want to use the parser here: http://developer.android.com/reference/org/json/JSONTokener.html

disatisfieddinosaur
  • 1,502
  • 10
  • 15
1

Given:

yourstring= {"Filename":"23476-love-823873.jpg","ChunkId":1,"ChunkLength":201929,"FileLength":12122} 

Try this Code:

    int startindex,endindex;
    startindex=indexOf(':');
    endindex=indexOf(',');
    String filename= yourstring.substring(startindex,endindex);
Abhishek
  • 874
  • 2
  • 8
  • 23
  • what if ChunkId comes first and then Filename? – Seshu Vinay Aug 15 '13 at 04:24
  • you may use regex to extract substring in that case but thats way too complicated.while creating the string its better to place markers around the string you want to extract..this will make your task of extracting easy.. as the length of filename changes we cannot use indexOf.. – Abhishek Aug 15 '13 at 04:31
  • So, isn't it better to use some kind of JSON handler? because the string looks like JSON. – Seshu Vinay Aug 15 '13 at 04:33
  • it may be.. i dont know much about json so i cant say anything about it... hence i suggested the procedure i knew.. – Abhishek Aug 15 '13 at 04:35
  • it is a JSON response, it is returned to me as a string – dolphinately Aug 15 '13 at 05:05
1

It looks like a structured object( HashMap, NameValuePair, JSON etc.).

Anyway, If its a String,

 String mString = "{\"Filename\":\"23476-love-823873.jpg\",\"ChunkId\":1,\"ChunkLength\":201929,\"FileLength\":12122}";



   if(mString.contains("Filename")){
            int start=mString.indexOf("Filename")+11;// If file name is not at beginning, have to do like this.
            int end=mString.indexOf(",", start)-1;//-1 for excluding a double code
            String filename=mString.substring(start, end);
        }
Nizam
  • 5,698
  • 9
  • 45
  • 57
0

It's look like a JSON string, there are many ways for you to parse it.

Refer to same question on SO: Sending and Parsing JSON Objects

Community
  • 1
  • 1
Krypton
  • 3,337
  • 5
  • 32
  • 52
0

The standard approach will be using a JSON library to de-serialize the String into a Java object or Map and then get the property value. If it is too heavy, you can use Regular Expression.

lawrencexu
  • 13
  • 4
0

then if that string is json response and you are sure that you will consistently get that string format then it is applicable to use the approach given by @Abhishek. See my sample below, same as what he gave.(a very little revision).

String sample = "{"Filename":"23476-love-823873.jpg","ChunkId":1,"ChunkLength":201929,"FileLength":12122}";

    Log.d("string", sample);
    int startindex,endindex;
    startindex = sample.indexOf(':');
    endindex   = sample.indexOf(',');
    String filename= sample.substring(startindex + 1,endindex);
    Log.d("result", filename);

your log will be like this:

string {"Filename":"23476-love-823873.jpg","ChunkId":1,"ChunkLength":201929,"FileLength":12122}
result "23476-love-823873.jpg"
lolliloop
  • 389
  • 8
  • 23