none
Regex .Split problem RRS feed

  • Question

  • Have the following string:

    { "TimeStamp": "10/2/2017 5:01:50 PM", "StormType": "S", "Count": 0, "Latitude": 0.0, "Longitude": 0.0, "TwentyLongitude": 0.0, "TwentyLatitude": 0.0, "FortyLatitude": 0.0, "FortyLongitude": 0.0, "TargetLatitude": 0.0, "TargetLongitude": 0.0, "StormHeading": 0, "LookatLatitude": 0.0, "LookatLongitude": 0.0, "$AzureWebJobsParentId": "499ec114-9fd4-4b73-aad0-8e64e3825d9b" }

    would like to get these fields and their values in separate variables. What would be the best approach for this? Thanks in Advance. 

    Tuesday, October 3, 2017 6:29 PM

Answers

  • Hello DDL19,

    >>would like to get these fields and their values in separate variables. What would be the best approach for this?

    As Sabah said,The best way I think is to parse string with JSON.You code could be like this.

    using Newtonsoft.Json.Linq;
    namespace Json_Problem
    { 
        class Program
        {
            static void Main(string[] args)
            {
                string jsonstr =@"{'TimeStamp': '10/2/2017 5:01:50 PM', 'StormType': 'S','Count': 0, 'Latitude': 0.0, 'Longitude': 0.0,'TwentyLongitude': 0.0, 'TwentyLatitude': 0.0,'FortyLatitude': 0.0, 'FortyLongitude': 0.0,
                                    'TargetLatitude': 0.0, 'TargetLongitude': 0.0, 'StormHeading': 0, 'LookatLatitude': 0.0, 'LookatLongitude': 0.0, '$AzureWebJobsParentId':'499ec114-9fd4-4b73-aad0-8e64e3825d9b'}";
    
              dynamic json= JObject.Parse(jsonstr);
    
              var a= json["TimeStamp"];
    
              var b = json["$AzureWebJobsParentId"];
    
            }
         } }

    Sincerely,

    neil hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by DDL19 Thursday, October 5, 2017 8:37 AM
    Thursday, October 5, 2017 8:33 AM
    Moderator

All replies

  • Hi DDL19,

    I believe this is a JSON output, why can't you just parse these JSON result I guess that would be easier to use a regular expression split. You could create a model like following, remember variable should be the same as in the JSON field. Hope this will help you.

    public DateTime TimeStamp{get; set;}
    public string StormType{get; set;}
    public int Count{get; set;}
    public float Latitude{get; set;}
    public float TwentyLongitude{get; set;}
    public float TwentyLatitude{get; set;}
    public float FortyLatitude{get; set;}
    public float FortyLongitude{get; set;}
    public float TargetLatitude{get; set;}
    public float TargetLongitude{get; set;}
    public int StormHeading{get; set;}
    public float LookatLatitude{get; set;}
    public float LookatLongitude{get; set;}
    public dateTime LookatLongitude{get; set;}
    public string AzureWebJobsParentId{get; set;}


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Tuesday, October 3, 2017 8:13 PM
    Moderator
  • How would I do this with JSON ? What would be the JObject syntax?
    Tuesday, October 3, 2017 8:40 PM
  • @DDL19 Is the string you provided in the question is a JSON output?

    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Tuesday, October 3, 2017 8:51 PM
    Moderator
  • @DDL19 there is an article on this topic, you can look into this:

    Working With JSON String In C#


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Tuesday, October 3, 2017 8:56 PM
    Moderator
  • Hello DDL19,

    >>would like to get these fields and their values in separate variables. What would be the best approach for this?

    As Sabah said,The best way I think is to parse string with JSON.You code could be like this.

    using Newtonsoft.Json.Linq;
    namespace Json_Problem
    { 
        class Program
        {
            static void Main(string[] args)
            {
                string jsonstr =@"{'TimeStamp': '10/2/2017 5:01:50 PM', 'StormType': 'S','Count': 0, 'Latitude': 0.0, 'Longitude': 0.0,'TwentyLongitude': 0.0, 'TwentyLatitude': 0.0,'FortyLatitude': 0.0, 'FortyLongitude': 0.0,
                                    'TargetLatitude': 0.0, 'TargetLongitude': 0.0, 'StormHeading': 0, 'LookatLatitude': 0.0, 'LookatLongitude': 0.0, '$AzureWebJobsParentId':'499ec114-9fd4-4b73-aad0-8e64e3825d9b'}";
    
              dynamic json= JObject.Parse(jsonstr);
    
              var a= json["TimeStamp"];
    
              var b = json["$AzureWebJobsParentId"];
    
            }
         } }

    Sincerely,

    neil hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by DDL19 Thursday, October 5, 2017 8:37 AM
    Thursday, October 5, 2017 8:33 AM
    Moderator