OpenStack OpenStack - 6 months ago 18
C# Question

Substring continue to search until specific character

I am getting a string at the run time. The string is in JSON format( key value pair). One of the key is "

". I need to retrieve the value of
. The problem is I don't know the position of "
" key. The string can look like
{"name":"XX", "userId":"YYY","age":"10"}
or it can look like
{"age":"10", "name":"XX", "userId":"YYY"}
or it can look like this

I am thinking of using

var index = myString.IndexOf("userId\":\"");
if(index != -1){
myString.Subtring(index, ???)//How to specify the length here

I am not sure, how to say continue until you find next
(double quote)


If only userId attribute is planned to use, you can simply declare an object with userId member and deserialize json. any other attributes will be omitted during deserialization.

class UserIDObj
   public string UserId { get; set; }

var obj = JsonConvert.DeserializeObject<UserIDObj>("{\"name\":\"XX\", \"userId\":\"YYY\",\"age\":\"10\"}");
string usrID = obj.UserId;