1

Hi How do I retrieve the number from the following string,

{"number":100,"data":[test]}

The number could be of any length.

I used the following code. but it gives and error message

strValue.Substring((strValue.IndexOf(":")+1), (strValue.IndexOf("data")));

the output comes like

100,"data":[

Thanks,

4
  • 3
    That looks like the start of a JSON string. Is that right? Commented Jun 29, 2012 at 15:54
  • yes. missed other bit. {"number":100,"data":test}
    – Joshua
    Commented Jun 29, 2012 at 15:55
  • Look at the output of each of those function calls, see if it helps. Post the results here if you are still unsure.
    – Guy
    Commented Jun 29, 2012 at 15:55
  • @Joshua I edited your question to fix the string. If it's wrong, let me know and I'll roll it back. Commented Jun 29, 2012 at 15:55

5 Answers 5

10

It looks like your input string is JSON. Is it? If so, you should use a proper JSON parser library like JSON.NET

3
  • Way to catch the underlying issue, rather than just the symptom! I suppose I should read up on using JSON, as I keep seeing people using it. Commented Jun 29, 2012 at 15:59
  • 3
    @JonSenchyna it's got pros and cons like anything else. Critically, it's much lighter on control characters than XML, and ECMAScript can treat it as a native object, so it's quite convenient for javascript and other ECMAScript implementations. Commented Jun 29, 2012 at 16:07
  • +1 Best approach if using JSON. Although if parsing a string in only one situation in code, the library might be a bit of unnecessary overhead.
    – StuperUser
    Commented Jun 29, 2012 at 16:26
3

As noted by Jon, your input string seems to be a JSON string which needs to be deserialized. You can write your own deserializer, or use an existing library, such as Json.NET. Here is an example:

string json = @"[
  {
    ""Name"": ""Product 1"",
    ""ExpiryDate"": ""\/Date(978048000000)\/"",
    ""Price"": 99.95,
    ""Sizes"": null
  },
  {
    ""Name"": ""Product 2"",
    ""ExpiryDate"": ""\/Date(1248998400000)\/"",
    ""Price"": 12.50,
    ""Sizes"": null
  }
]";

List<Product> products = JsonConvert.DeserializeObject<List<Product>>(json);
3

Your attempt is close. There are two (possibly three issues) I found.

  1. Your string has a comma after the number you're looking for. Since your code is searching for the index of "data", your end index will end up one character too far.
  2. The second paramter of String.Substring(int, int) is actually a length, not an end index.
  3. Strings are immutable in C#. Because of this, none of the member functions on a string actually modify its value. Instead, they return a new value. I don't know if your code example is complete, so you may be assigning the return value of SubString to something, but if you're not, the end result is that strValue remains unchanged.

Overall, the result of your current call to string.Substring is returning 100,"data":[tes. (and as far as I can see, it's not storing the result).

Try the following code:

string justTheNumber = null;
// Make sure we get the correct ':'
int startIndex = strValue.IndexOf("\"number\":") + 9;
// Search for the ',' that comes after "number":
int endIndex = strValue.IndexOf(',', startIndex);
int length = endIndex - startIndex;
// Note, we could potentially get an ArguementOutOfRangeException here.
// You'll want to handle cases where startPosition < 0 or length < 0.
string justTheNumber  = strValue.Substring(startIndex, length);

Note: This solution does not handle if "number": is the last entry in the list inside your string, but it should handle every other placement in it.

If your strings get more complex, you could try using Regular Expressions to perform your searches.

2
  • If you use strValue.IndexOf(",")), would that return the position where the comma is? Shouldn't you pas the length of the substring you want instead? Like strValue.IndexOf(",") - strValue.IndexOf(":").
    – comecme
    Commented Jun 29, 2012 at 16:41
  • That's what I get for not double-checking the OP's code for other issues aside from the first obvious one I find. You are 100% correct. The original code would find the string starting at ":", and the following IndexOf(",") characters. I'll update the above right away. Commented Jun 29, 2012 at 17:04
2

Parsing JSON spring in that way is very bad practice as everything is hardcoded. Have you though of using 3rd party library for parsing JSON strings, like Newtonsoft JSON.

0

I guess you needed to use IndexOf(",") istead of IndexOf("data")

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.