Parse a string array in json response [closed]












-3














I have to accept the json response in below format which can have many "fields" in the string array. What would be the simplest way to parse this in a loop where I can extract "fieldvalues" and do some operations on them?



 {
"fields":
[
"/field1/{field1value}/field2/{field2value}/field3/{field3value}",
"/field1/{field1value}/field2/{field2value}/field3/{field3value}"
]
}









share|improve this question













closed as unclear what you're asking by Ahmed Abdelhameed, Camilo Terevinto, Dour High Arch, user6655984, Tetsuya Yamamoto Nov 12 at 3:06


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.















  • What is your problem? Reading the json as an array with two strings or processing the content of these two strings? As usual a piece of code with your attempt to solve the problem is the best way to explain the problem
    – Steve
    Nov 11 at 23:37












  • If you mean "parsing JSON": Don't do it, Json.NET does this for you! If you mean "parse a string in an array" then this question is not related to JSON at all.
    – Olivier Jacot-Descombes
    Nov 11 at 23:46










  • @Steve I'll try to ask this better, it's both. My usual approach is receive the response as JObject and then do $"{json["fields"]}" to get the string, but in this case that'll give me a huge string. I looked up JArray.Parse, but couldn't make that work
    – user1097128
    Nov 11 at 23:55


















-3














I have to accept the json response in below format which can have many "fields" in the string array. What would be the simplest way to parse this in a loop where I can extract "fieldvalues" and do some operations on them?



 {
"fields":
[
"/field1/{field1value}/field2/{field2value}/field3/{field3value}",
"/field1/{field1value}/field2/{field2value}/field3/{field3value}"
]
}









share|improve this question













closed as unclear what you're asking by Ahmed Abdelhameed, Camilo Terevinto, Dour High Arch, user6655984, Tetsuya Yamamoto Nov 12 at 3:06


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.















  • What is your problem? Reading the json as an array with two strings or processing the content of these two strings? As usual a piece of code with your attempt to solve the problem is the best way to explain the problem
    – Steve
    Nov 11 at 23:37












  • If you mean "parsing JSON": Don't do it, Json.NET does this for you! If you mean "parse a string in an array" then this question is not related to JSON at all.
    – Olivier Jacot-Descombes
    Nov 11 at 23:46










  • @Steve I'll try to ask this better, it's both. My usual approach is receive the response as JObject and then do $"{json["fields"]}" to get the string, but in this case that'll give me a huge string. I looked up JArray.Parse, but couldn't make that work
    – user1097128
    Nov 11 at 23:55
















-3












-3








-3







I have to accept the json response in below format which can have many "fields" in the string array. What would be the simplest way to parse this in a loop where I can extract "fieldvalues" and do some operations on them?



 {
"fields":
[
"/field1/{field1value}/field2/{field2value}/field3/{field3value}",
"/field1/{field1value}/field2/{field2value}/field3/{field3value}"
]
}









share|improve this question













I have to accept the json response in below format which can have many "fields" in the string array. What would be the simplest way to parse this in a loop where I can extract "fieldvalues" and do some operations on them?



 {
"fields":
[
"/field1/{field1value}/field2/{field2value}/field3/{field3value}",
"/field1/{field1value}/field2/{field2value}/field3/{field3value}"
]
}






c# .net json json.net






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 23:34









user1097128

289




289




closed as unclear what you're asking by Ahmed Abdelhameed, Camilo Terevinto, Dour High Arch, user6655984, Tetsuya Yamamoto Nov 12 at 3:06


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






closed as unclear what you're asking by Ahmed Abdelhameed, Camilo Terevinto, Dour High Arch, user6655984, Tetsuya Yamamoto Nov 12 at 3:06


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • What is your problem? Reading the json as an array with two strings or processing the content of these two strings? As usual a piece of code with your attempt to solve the problem is the best way to explain the problem
    – Steve
    Nov 11 at 23:37












  • If you mean "parsing JSON": Don't do it, Json.NET does this for you! If you mean "parse a string in an array" then this question is not related to JSON at all.
    – Olivier Jacot-Descombes
    Nov 11 at 23:46










  • @Steve I'll try to ask this better, it's both. My usual approach is receive the response as JObject and then do $"{json["fields"]}" to get the string, but in this case that'll give me a huge string. I looked up JArray.Parse, but couldn't make that work
    – user1097128
    Nov 11 at 23:55




















  • What is your problem? Reading the json as an array with two strings or processing the content of these two strings? As usual a piece of code with your attempt to solve the problem is the best way to explain the problem
    – Steve
    Nov 11 at 23:37












  • If you mean "parsing JSON": Don't do it, Json.NET does this for you! If you mean "parse a string in an array" then this question is not related to JSON at all.
    – Olivier Jacot-Descombes
    Nov 11 at 23:46










  • @Steve I'll try to ask this better, it's both. My usual approach is receive the response as JObject and then do $"{json["fields"]}" to get the string, but in this case that'll give me a huge string. I looked up JArray.Parse, but couldn't make that work
    – user1097128
    Nov 11 at 23:55


















What is your problem? Reading the json as an array with two strings or processing the content of these two strings? As usual a piece of code with your attempt to solve the problem is the best way to explain the problem
– Steve
Nov 11 at 23:37






What is your problem? Reading the json as an array with two strings or processing the content of these two strings? As usual a piece of code with your attempt to solve the problem is the best way to explain the problem
– Steve
Nov 11 at 23:37














If you mean "parsing JSON": Don't do it, Json.NET does this for you! If you mean "parse a string in an array" then this question is not related to JSON at all.
– Olivier Jacot-Descombes
Nov 11 at 23:46




If you mean "parsing JSON": Don't do it, Json.NET does this for you! If you mean "parse a string in an array" then this question is not related to JSON at all.
– Olivier Jacot-Descombes
Nov 11 at 23:46












@Steve I'll try to ask this better, it's both. My usual approach is receive the response as JObject and then do $"{json["fields"]}" to get the string, but in this case that'll give me a huge string. I looked up JArray.Parse, but couldn't make that work
– user1097128
Nov 11 at 23:55






@Steve I'll try to ask this better, it's both. My usual approach is receive the response as JObject and then do $"{json["fields"]}" to get the string, but in this case that'll give me a huge string. I looked up JArray.Parse, but couldn't make that work
– user1097128
Nov 11 at 23:55














2 Answers
2






active

oldest

votes


















1














If you just need to parse the JSON and iterate over the strings in the fields array, you can do that like this:



JObject obj = JObject.Parse(json);
foreach (var path in obj["fields"].Values<string>())
{
Console.WriteLine(path);
}


If you need to break down the strings into name-value pairs, you can split on / and loop over the parts like this (inside the previous loop):



    string parts = path.Split('/');
for (var i = 1; i < parts.Length; i += 2)
{
var name = parts[i];
var value = (i + 1 < parts.Length) ? parts[i + 1] : null;

Console.WriteLine(name + ": " + value);
}


See fiddle here: https://dotnetfiddle.net/TupvAu






share|improve this answer





















  • Thank you! This is really helpful, I was looking for something like Values function.
    – user1097128
    Nov 12 at 2:02



















2














You can parse that Json and obtain the single lines of data in this way



// As an example for your input data.....
string json = "{"fields":["/field1/{field1value}/field2/{field2value}/field3/{field3value}","/field1/{field1value}/field2/{field2value}/field3/{field3value}"]}";

// Json.NET JObject.Parse
var lines = JObject.Parse(json);
// Remove comment with LinqPad
// lines.GetType().Dump();
// Not really needed to explicitly use the Properties method...
foreach (var x in lines.Properties())
{
// Remove comment with LinqPad
// x.GetType().Dump();
foreach (var k in x.Value)
{
// Remove comment with LinqPad
// k.GetType().Dump();
string dataLine = k.Value<string>();
Console.WriteLine(dataLine);
}
}


I suggest to try this code with LinqPad using the Dump method on the variables lines, x and k to understand the inner structure of that JObject returned by Parse.






share|improve this answer






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    If you just need to parse the JSON and iterate over the strings in the fields array, you can do that like this:



    JObject obj = JObject.Parse(json);
    foreach (var path in obj["fields"].Values<string>())
    {
    Console.WriteLine(path);
    }


    If you need to break down the strings into name-value pairs, you can split on / and loop over the parts like this (inside the previous loop):



        string parts = path.Split('/');
    for (var i = 1; i < parts.Length; i += 2)
    {
    var name = parts[i];
    var value = (i + 1 < parts.Length) ? parts[i + 1] : null;

    Console.WriteLine(name + ": " + value);
    }


    See fiddle here: https://dotnetfiddle.net/TupvAu






    share|improve this answer





















    • Thank you! This is really helpful, I was looking for something like Values function.
      – user1097128
      Nov 12 at 2:02
















    1














    If you just need to parse the JSON and iterate over the strings in the fields array, you can do that like this:



    JObject obj = JObject.Parse(json);
    foreach (var path in obj["fields"].Values<string>())
    {
    Console.WriteLine(path);
    }


    If you need to break down the strings into name-value pairs, you can split on / and loop over the parts like this (inside the previous loop):



        string parts = path.Split('/');
    for (var i = 1; i < parts.Length; i += 2)
    {
    var name = parts[i];
    var value = (i + 1 < parts.Length) ? parts[i + 1] : null;

    Console.WriteLine(name + ": " + value);
    }


    See fiddle here: https://dotnetfiddle.net/TupvAu






    share|improve this answer





















    • Thank you! This is really helpful, I was looking for something like Values function.
      – user1097128
      Nov 12 at 2:02














    1












    1








    1






    If you just need to parse the JSON and iterate over the strings in the fields array, you can do that like this:



    JObject obj = JObject.Parse(json);
    foreach (var path in obj["fields"].Values<string>())
    {
    Console.WriteLine(path);
    }


    If you need to break down the strings into name-value pairs, you can split on / and loop over the parts like this (inside the previous loop):



        string parts = path.Split('/');
    for (var i = 1; i < parts.Length; i += 2)
    {
    var name = parts[i];
    var value = (i + 1 < parts.Length) ? parts[i + 1] : null;

    Console.WriteLine(name + ": " + value);
    }


    See fiddle here: https://dotnetfiddle.net/TupvAu






    share|improve this answer












    If you just need to parse the JSON and iterate over the strings in the fields array, you can do that like this:



    JObject obj = JObject.Parse(json);
    foreach (var path in obj["fields"].Values<string>())
    {
    Console.WriteLine(path);
    }


    If you need to break down the strings into name-value pairs, you can split on / and loop over the parts like this (inside the previous loop):



        string parts = path.Split('/');
    for (var i = 1; i < parts.Length; i += 2)
    {
    var name = parts[i];
    var value = (i + 1 < parts.Length) ? parts[i + 1] : null;

    Console.WriteLine(name + ": " + value);
    }


    See fiddle here: https://dotnetfiddle.net/TupvAu







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 12 at 1:47









    Brian Rogers

    73.6k17185199




    73.6k17185199












    • Thank you! This is really helpful, I was looking for something like Values function.
      – user1097128
      Nov 12 at 2:02


















    • Thank you! This is really helpful, I was looking for something like Values function.
      – user1097128
      Nov 12 at 2:02
















    Thank you! This is really helpful, I was looking for something like Values function.
    – user1097128
    Nov 12 at 2:02




    Thank you! This is really helpful, I was looking for something like Values function.
    – user1097128
    Nov 12 at 2:02













    2














    You can parse that Json and obtain the single lines of data in this way



    // As an example for your input data.....
    string json = "{"fields":["/field1/{field1value}/field2/{field2value}/field3/{field3value}","/field1/{field1value}/field2/{field2value}/field3/{field3value}"]}";

    // Json.NET JObject.Parse
    var lines = JObject.Parse(json);
    // Remove comment with LinqPad
    // lines.GetType().Dump();
    // Not really needed to explicitly use the Properties method...
    foreach (var x in lines.Properties())
    {
    // Remove comment with LinqPad
    // x.GetType().Dump();
    foreach (var k in x.Value)
    {
    // Remove comment with LinqPad
    // k.GetType().Dump();
    string dataLine = k.Value<string>();
    Console.WriteLine(dataLine);
    }
    }


    I suggest to try this code with LinqPad using the Dump method on the variables lines, x and k to understand the inner structure of that JObject returned by Parse.






    share|improve this answer




























      2














      You can parse that Json and obtain the single lines of data in this way



      // As an example for your input data.....
      string json = "{"fields":["/field1/{field1value}/field2/{field2value}/field3/{field3value}","/field1/{field1value}/field2/{field2value}/field3/{field3value}"]}";

      // Json.NET JObject.Parse
      var lines = JObject.Parse(json);
      // Remove comment with LinqPad
      // lines.GetType().Dump();
      // Not really needed to explicitly use the Properties method...
      foreach (var x in lines.Properties())
      {
      // Remove comment with LinqPad
      // x.GetType().Dump();
      foreach (var k in x.Value)
      {
      // Remove comment with LinqPad
      // k.GetType().Dump();
      string dataLine = k.Value<string>();
      Console.WriteLine(dataLine);
      }
      }


      I suggest to try this code with LinqPad using the Dump method on the variables lines, x and k to understand the inner structure of that JObject returned by Parse.






      share|improve this answer


























        2












        2








        2






        You can parse that Json and obtain the single lines of data in this way



        // As an example for your input data.....
        string json = "{"fields":["/field1/{field1value}/field2/{field2value}/field3/{field3value}","/field1/{field1value}/field2/{field2value}/field3/{field3value}"]}";

        // Json.NET JObject.Parse
        var lines = JObject.Parse(json);
        // Remove comment with LinqPad
        // lines.GetType().Dump();
        // Not really needed to explicitly use the Properties method...
        foreach (var x in lines.Properties())
        {
        // Remove comment with LinqPad
        // x.GetType().Dump();
        foreach (var k in x.Value)
        {
        // Remove comment with LinqPad
        // k.GetType().Dump();
        string dataLine = k.Value<string>();
        Console.WriteLine(dataLine);
        }
        }


        I suggest to try this code with LinqPad using the Dump method on the variables lines, x and k to understand the inner structure of that JObject returned by Parse.






        share|improve this answer














        You can parse that Json and obtain the single lines of data in this way



        // As an example for your input data.....
        string json = "{"fields":["/field1/{field1value}/field2/{field2value}/field3/{field3value}","/field1/{field1value}/field2/{field2value}/field3/{field3value}"]}";

        // Json.NET JObject.Parse
        var lines = JObject.Parse(json);
        // Remove comment with LinqPad
        // lines.GetType().Dump();
        // Not really needed to explicitly use the Properties method...
        foreach (var x in lines.Properties())
        {
        // Remove comment with LinqPad
        // x.GetType().Dump();
        foreach (var k in x.Value)
        {
        // Remove comment with LinqPad
        // k.GetType().Dump();
        string dataLine = k.Value<string>();
        Console.WriteLine(dataLine);
        }
        }


        I suggest to try this code with LinqPad using the Dump method on the variables lines, x and k to understand the inner structure of that JObject returned by Parse.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 12 at 0:06

























        answered Nov 12 at 0:00









        Steve

        179k16152216




        179k16152216















            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues