Retrieve the values from json string

后端 未结 5 803
忘掉有多难
忘掉有多难 2021-01-26 05:33

I have json string. I want to retrieve the contact from json string. Following json contains array of contacts. here is my json string

5条回答
  •  误落风尘
    2021-01-26 06:06

    Firstly, I think you'll find that your JSON is not well-formed. You don't need the extra commas after the two "created" dates and there is a right-square bracket missing before the second last curly brace. I recommend you always validate your JSON using this awesome site: http://jsonformatter.curiousconcept.com

    Secondly, you are not referencing the array elements correctly. Although I agree with @grundy that you should be creating class definitions for managing JSON and using LINQ, there is absolutely nothing wrong with the Newtonsoft library. You can still persist with your method.

    Try this:-

    var json = JObject.Parse(returnStr);
    var fields = (JArray)json["contacts"]["contact"][0]["fields"];
    
    var givenName = fields[0]["value"]["givenName"];
    var familyName = fields[0]["value"]["familyName"];
    var email = fields[1]["value"];
    

提交回复
热议问题