-2

How can I extract the float variable from a string in a JSON file?

This is an example of a possible string (characters stop after the comma):

            "x": "1.234567",

The string has 12 blank spaces before the double quotes and the number I need to extract is 1.234567

The decimals after the dot could be more than 6.

The double quotes are also in the string, while the final comma is not always there

Alessandro
  • 103
  • 3
  • 11
  • 5
    Is that the **entire** string? Or is it perhaps part of a larger JSON string? – mjwills Jul 28 '19 at 11:11
  • `How can I extract a double from this strings?` There are two doubles in the string? Do you want the first one or the second one? – mjwills Jul 28 '19 at 11:14
  • The double is just one, it's 1.234567. I'm reading from a file, and before "x" I have 12 spaces – Alessandro Jul 28 '19 at 11:40
  • a) You didn't answer my first comment. b) Let's say the text was `"x": "1.234568", "x": "1.234567"` - what result are you hoping to receive? – mjwills Jul 28 '19 at 11:42
  • I think you need to read about JSON serialization. From your input it seems that the issue is to get the right substring of a bigger json-string and has nothing to do with a `bool`. Take a look at the [json.net library](https://www.newtonsoft.com/json) or the [new json api in .net core 3](https://devblogs.microsoft.com/dotnet/try-the-new-system-text-json-apis/). – Joelius Jul 28 '19 at 12:43
  • @Joelius I know about Json serialization but now I'm trying a different method. I have a structure that's nframe{nperson{njoint{x, y, z}}} but I want to read it as nperson{nframe{njoint{x, y, z}}}. In the future I might try json serialization again, but for now I just need to know the answer to my question. Thank you anyway :) – Alessandro Jul 28 '19 at 12:55

4 Answers4

2

If your strings take that format, you can split on the double-quote and extract the number from the resulting array:

    var str = "      \"x\":\"123.456\""; //example
    var number = Convert.ToDouble(str.Split('"')[3]);
iakobski
  • 1,000
  • 7
  • 8
1

I think this code helps:

Convert.ToDouble("1.234567")

or put your string variable in it.

  • 1
    The OP's problem is largely how to **extract** the string. – mjwills Jul 28 '19 at 11:42
  • The problem is extracting that from the whole string. And because the length of the number is not always the same, I can't convert it to an array and pick the numbers in their positions – Alessandro Jul 28 '19 at 11:47
  • Indeed you can convert it to an array - not an array of chars but an array of strings - using string.Split, see my second answer. – iakobski Jul 28 '19 at 14:37
0

A simple regular expression can pull the number out of the string:

    var str = "      \"x\": \"1.234567\"";  //example
    var regex = new Regex("[\\d.]+");
    var match = regex.Match(str).Groups[0].Value;
    var number = Convert.ToDouble(match);

Explanation: the regex matches the first set of digits and dots together and puts it in Group[0]. If your possible strings can contain digits or dots anywhere else you will need to modify the regular expression to pinpoint the number you want. But this satisfies the examples in your question.

iakobski
  • 1,000
  • 7
  • 8
  • Thank you. I couldn't ask for a better answer! I would up vote you, if only I didn't have a reputation so low. – Alessandro Jul 28 '19 at 14:35
0

In case you don't want to use Regex, and if the formatting of the string is always in that form, you can do:

public string myString= "\"x\": \"1.234568\"";
public string[] subStrings;

void Start()
{

    subStrings = myString.Split('\"');
    float myFloat = Convert.ToSingle(subStrings[3]);

}

The method myString.Split(char[]) returns a strings array, splitting your stringi everywhere it finds the char you pass as separator. If your formatting is always done like that, the 4th element in the subString[] is your number.

kefren
  • 1,042
  • 7
  • 12
  • other than the fact this doesn't compile, it basically duplicates my answer from earlier. Also you don't need to escape a double quote inside single quotes. And the OP said they were two example strings, not one long one. – iakobski Jul 28 '19 at 14:47
  • @iakobski, thank you very much for your constructive observations. As you can see, I posted the answer at the same time as yours, I just ended a couple minutes later… But if it offends you I can delete it. I was mistaken on the format of the string, because the 2 example strings were identical...so I though ti would have been a single string. – kefren Jul 28 '19 at 14:53
  • No, leave your answer to my question, I'm glad you took the time for my problem. Thank you! – Alessandro Jul 28 '19 at 15:42