C#,一个字符串';s Split()方法从字符串中获取某些信息

C#,一个字符串';s Split()方法从字符串中获取某些信息,c#,C#,我在我的C#应用程序中得到了这个响应,我正试图获取“ID”,即12345678 在{“数据”:{“用户”:{“id”:“12345678”…诸如此类 我写了一个代码,但它不工作 string value=httpreqResp.Split(新[]{”/“id/”:/“},StringSplitOptions.None)[1]。Split(“”)[0]; 如果可能,有人能帮忙吗 您可以转到并粘贴json 您可以设置一些其他选项,例如根类的名称 它会生成一堆C#-我本可以用你的json来实现这一点,

我在我的C#应用程序中得到了这个响应,我正试图获取“ID”,即12345678 在{“数据”:{“用户”:{“id”:“12345678”…诸如此类

我写了一个代码,但它不工作

string value=httpreqResp.Split(新[]{”/“id/”:/“},StringSplitOptions.None)[1]。Split(“”)[0];
如果可能,有人能帮忙吗
  • 您可以转到并粘贴json
  • 您可以设置一些其他选项,例如根类的名称
  • 它会生成一堆C#-我本可以用你的json来实现这一点,但我在手机上,QTIO在手机上无法工作;无法粘贴json,因此我不得不使用示例temperatures.json
  • 生成的C#如下所示,一系列类表示JSON中的对象,然后是一些方便的从JSON转换到JSON的一次性方法:
  • 并将被用作:

    // Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse); 
        public class SubscriptionProduct    {
            public string id { get; set; } 
            public string emoteSetID { get; set; } 
            public string name { get; set; } 
            public string __typename { get; set; } 
        }
    
        public class User    {
            public string id { get; set; } 
            public string displayName { get; set; } 
            public List<SubscriptionProduct> subscriptionProducts { get; set; } 
            public object self { get; set; } 
            public string __typename { get; set; } 
        }
    
        public class RequestInfo    {
            public string countryCode { get; set; } 
            public string __typename { get; set; } 
        }
    
        public class Data    {
            public User user { get; set; } 
            public object currentUser { get; set; } 
            public RequestInfo requestInfo { get; set; } 
        }
    
        public class Extensions    {
            public int durationMilliseconds { get; set; } 
            public string operationName { get; set; } 
            public string requestID { get; set; } 
        }
    
        public class MyArray    {
            public Data data { get; set; } 
            public Extensions extensions { get; set; } 
        }
    
        public class Root    {
            public List<MyArray> MyArray { get; set; } 
        }
    
    Root myDeserializedClass=JsonConvert.DeserializeObject(responseStringFromWebservice);
    var id=myDeserializedClass.MyArray[0]。Data.User.id;
    
    这是一个JSON字符串

    您可以将JsonSerializer.Deserialize转换为C#类

    首先创建自己的类来存储数据,如下所示

    Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(responseStringFromWebservice); 
    var id = myDeserializedClass.MyArray[0].Data.User.id;
    
    公共类订阅产品{
    公共字符串id{get;set;}
    公共字符串emoteSetID{get;set;}
    公共字符串名称{get;set;}
    公共字符串uuTypeName{get;set;}
    }
    公共类用户{
    公共字符串id{get;set;}
    公共字符串displayName{get;set;}
    公共列表订阅产品{get;set;}
    公共对象自{get;set;}
    公共字符串uuTypeName{get;set;}
    }
    公共类请求信息{
    公共字符串countryCode{get;set;}
    公共字符串uuTypeName{get;set;}
    }
    公共类数据{
    公共用户{get;set;}
    公共对象currentUser{get;set;}
    public RequestInfo RequestInfo{get;set;}
    }
    公共类扩展{
    公共int持续毫秒数{get;set;}
    公共字符串操作名{get;set;}
    公共字符串requestID{get;set;}
    }
    公共类MyArray{
    公共数据数据{get;set;}
    公共扩展{get;set;}
    }
    公共类MyClass{
    公共列表MyArray{get;set;}
    }
    
    然后用这个:

        public class SubscriptionProduct    {
            public string id { get; set; } 
            public string emoteSetID { get; set; } 
            public string name { get; set; } 
            public string __typename { get; set; } 
        }
    
        public class User    {
            public string id { get; set; } 
            public string displayName { get; set; } 
            public List<SubscriptionProduct> subscriptionProducts { get; set; } 
            public object self { get; set; } 
            public string __typename { get; set; } 
        }
    
        public class RequestInfo    {
            public string countryCode { get; set; } 
            public string __typename { get; set; } 
        }
    
        public class Data    {
            public User user { get; set; } 
            public object currentUser { get; set; } 
            public RequestInfo requestInfo { get; set; } 
        }
    
        public class Extensions    {
            public int durationMilliseconds { get; set; } 
            public string operationName { get; set; } 
            public string requestID { get; set; } 
        }
    
        public class MyArray    {
            public Data data { get; set; } 
            public Extensions extensions { get; set; } 
        }
    
        public class MyClass {
            public List<MyArray> MyArray { get; set; } 
        }
    
    var mydata=JsonSerializer.Deserialize(jsonString);
    
    这是JSON,为什么不将其解析为JSON?我已经解析过了,我只需要将其拆分,然后从整个字符串中获取12345678部分。您是如何解析它的?您只需将数据作为
    字符串
    。将其加载到POCO中或使用类似JSON.Net的Linq API(
    JObject
    )之类的东西。“我已经解析过了”-不,如果您要求拆分字符串,您没有。如果您对其进行了解析,您已经得到了所需的结果。@Godlike-这不是解析。您只是得到了原始响应。请使用OP的JSON文本演示如何解析OP要求的值。我想解释正则表达式或拆分字符串。JSON字符串class e.t.c太难理解,每次都太不方便了,如果你能帮忙的话,那就太好了。如果不行,也可以。谢谢你,谢谢你,我的代码会正常工作,但是你的否决票,提示其他访问者JSON不是正确答案:)。我没有否决,但问题是我不喜欢使用Json。
    Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(responseStringFromWebservice); 
    var id = myDeserializedClass.MyArray[0].Data.User.id;
    
        public class SubscriptionProduct    {
            public string id { get; set; } 
            public string emoteSetID { get; set; } 
            public string name { get; set; } 
            public string __typename { get; set; } 
        }
    
        public class User    {
            public string id { get; set; } 
            public string displayName { get; set; } 
            public List<SubscriptionProduct> subscriptionProducts { get; set; } 
            public object self { get; set; } 
            public string __typename { get; set; } 
        }
    
        public class RequestInfo    {
            public string countryCode { get; set; } 
            public string __typename { get; set; } 
        }
    
        public class Data    {
            public User user { get; set; } 
            public object currentUser { get; set; } 
            public RequestInfo requestInfo { get; set; } 
        }
    
        public class Extensions    {
            public int durationMilliseconds { get; set; } 
            public string operationName { get; set; } 
            public string requestID { get; set; } 
        }
    
        public class MyArray    {
            public Data data { get; set; } 
            public Extensions extensions { get; set; } 
        }
    
        public class MyClass {
            public List<MyArray> MyArray { get; set; } 
        }
    
    var mydata = JsonSerializer.Deserialize<MyClass>(jsonString);