在c#.net中使用字符串
嗨,我如何从以下字符串中检索数字在c#.net中使用字符串,c#,asp.net,.net,string,substring,C#,Asp.net,.net,String,Substring,嗨,我如何从以下字符串中检索数字 {"number":100,"data":[test]} 数字可以是任意长度 我使用了以下代码。但它给出了错误信息 strValue.Substring((strValue.IndexOf(":")+1), (strValue.IndexOf("data"))); 输出结果如下所示 100,“数据”:[ 谢谢,看起来你的输入字符串是JSON。是吗?如果是这样,你应该使用一个合适的JSON解析器库,比如说你的输入字符串是JSON。是吗?如果是这样,你应该使用一
{"number":100,"data":[test]}
数字可以是任意长度
我使用了以下代码。但它给出了错误信息
strValue.Substring((strValue.IndexOf(":")+1), (strValue.IndexOf("data")));
输出结果如下所示
100,“数据”:[
谢谢,看起来你的输入字符串是JSON。是吗?如果是这样,你应该使用一个合适的JSON解析器库,比如说你的输入字符串是JSON。是吗?如果是这样,你应该使用一个合适的JSON解析器库,比如说我想你需要使用IndexOf(“,”)istead of IndexOf(“data”)我想你需要使用IndexOf(“,”)istead of IndexOf(“data”)您的尝试已接近尾声。我发现了两个(可能有三个问题)
子字符串的返回值指定给某个对象,但如果不完整,则最终结果是strValue
保持不变
string.Substring
的结果返回100,“数据”:[tes
(据我所知,它没有存储结果)
请尝试以下代码:
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);
string justTheNumber=null;
//确保我们得到正确的“:”
int startIndex=strValue.IndexOf(“\”number\:”)+9;
//搜索“number”后面的“,”:
int-endIndex=strValue.IndexOf(',',startIndex);
int length=endIndex-startIndex;
//注意,我们可能会在这里得到ArguementOutOfRangeException。
//您需要处理起始位置<0或长度<0的情况。
字符串justTheNumber=strValue.Substring(startIndex,长度);
注意:如果“number”:
是字符串中列表中的最后一个条目,则此解决方案不处理,但它应处理其中的每个其他位置
如果字符串变得更复杂,您可以尝试使用来执行搜索。您的尝试已接近尾声。我发现了两个问题(可能有三个问题)
子字符串的返回值指定给某个对象,但如果不完整,则最终结果是strValue
保持不变
string.Substring
的结果返回100,“数据”:[tes
(据我所知,它没有存储结果)
请尝试以下代码:
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);
string justTheNumber=null;
//确保我们得到正确的“:”
int startIndex=strValue.IndexOf(“\”number\:”)+9;
//搜索“number”后面的“,”:
int-endIndex=strValue.IndexOf(',',startIndex);
int length=endIndex-startIndex;
//注意,我们可能会在这里得到ArguementOutOfRangeException。
//您需要处理起始位置<0或长度<0的情况。
字符串justTheNumber=strValue.Substring(startIndex,长度);
注意:如果“number”:
是字符串中列表中的最后一个条目,则此解决方案不处理,但它应处理其中的每个其他位置
如果字符串变得更复杂,您可以尝试使用来执行搜索。以这种方式解析JSON spring是非常糟糕的做法,因为所有内容都是硬编码的。您是否想过使用第三方库解析JSON字符串,例如。以这种方式解析JSON spring是非常糟糕的做法,因为所有内容都是硬编码的。您是否想过使用第三方库解析JSON字符串,如。如Jon所述,您的输入字符串似乎是需要反序列化的JSON字符串。您可以编写自己的反序列化程序,或使用现有库,如。以下是一个示例:
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);
stringjson=@”[
{
“名称”:“产品1”,
“ExpiryDate”:“\/日期(978048000000)\/”,
“价格”:99.95,
“”大小“”:空
},
{
“名称”:“产品2”,
“ExpiryDate”“:”“\/日期(1248998400000)\/”,
“价格”:12.50,
“”大小“”:空
}
]";
List products=JsonConvert.DeserializeObject(json);
正如Jon所指出的,您的输入字符串似乎是需要反序列化的JSON字符串。您可以编写自己的反序列化程序,或使用现有库,例如。以下是一个示例:
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);
stringjson=@”[
{
“名称”:“产品1”,
“ExpiryDate”:“\/日期(978048000000)\/”,
“价格”:99.95,
“”大小“”:空
},
{
“名称”:“产品2”,
“ExpiryDate”“:”“\/日期(1248998400000)\/”,
“价格”:12.50,
“”大小“”:空
}
]";
List products=JsonConvert.DeserializeObject(json);
这看起来像JSON字符串的开头。对吗?是的。缺少其他位。{“number”:100,“data”:test}查看每个函数调用的输出,看看是否有帮助。如果您仍然不确定,请在此处发布结果。@Joshua我编辑了您的问题以修复字符串。如果错误,请告诉我,我将回滚。这看起来像是JSON字符串的开始。是吗?是的。缺少其他位。{“number”:100,“data”:test}查看每个函数调用的输出,看看是否有帮助。如果您仍然不确定,请将结果发布在此处。@Joshua我编辑了您的问题以修复字符串。如果错误,请告诉我,我将回滚它。这是抓住潜在问题的方法,而不仅仅是症状!我想我应该阅读usin