Turn string into json C#

learning...

I have this sample code that i am working with. The json is a result of the http post.

var json = @"{'user': {
                        'country':'US',
                        'email':'[email protected]',
                        'first_name':'Test',
                        'last_name':'API',
                        'phone':null,
                        'zip':null,
                        'login_url':'https://new.site.com/xlogin/12325/abd9832cd92'
                        }
                    }";
        var jsonSerializer = new JavaScriptSerializer();
        var itemsList = (IDictionary<string, object>)jsonSerializer.DeserializeObject(json);
        var url = itemsList["user.login_url"];

On itemsList["user.login_url"] i am getting the following error:

 The given key was not present in the dictionary.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.

Source Error:


Line 545:        var jsonSerializer = new JavaScriptSerializer();
Line 546:        var itemsList = (IDictionary<string, object>)jsonSerializer.DeserializeObject(json);
Line 547:        var url = itemsList["user.login_url"];
Line 548:    }
Line 549:

Am i doing something wrong here? How should i access the first name, last name and url etc from this object? enter image description here

Alternately, how can i tie this result to a class that has following properties? I just need a pointer to a good resource.

public class User
{
    public string Country { get; set; }
    public string Email { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Phone { get; set; }
    public string Zip { get; set; }
    public string LoginUrl { get; set; }
}

Thanks.

Stu

"user.login_url" is the property path you'd expect to use in JavaScript ... try accessing the dictionary keys instead

var user = itemsList["user"] as IDictionary<string,object>;
var url = user["login_url"] as string;

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

C++ turn a std::tuple<char, char, char> into a std::string?

분류에서Dev

Turn comma seperated string into an array

분류에서Dev

C # OpenApi Json String 응답

분류에서Dev

C# JSON Rest Response String

분류에서Dev

Convert dynamic csv to json containing List<string> C#

분류에서Dev

How to assign JSON array to a string using the SimpleJSON library in C++

분류에서Dev

JSON serialize a custom C# struct to a string, not an Object

분류에서Dev

How can I turn a delimited string into a nested hashtable in PowerShell?

분류에서Dev

How to turn a hclust-object into JSON for D3?

분류에서Dev

JSON parseError - get json as string

분류에서Dev

JSON을 C #의 Dictionary <string, List <string >>으로 역 직렬화하려고합니다.

분류에서Dev

Как предотвратить инициализацию свойств, которых нет в строке JSON во время десериализации в C #?

분류에서Dev

Convert String to JSON in bash

분류에서Dev

Deserialise json form string

분류에서Dev

List of Objects To Json String

분류에서Dev

JSON error: Expecting 'STRING'

분류에서Dev

Parse a JSON String and display it

분류에서Dev

string, int 배열로 json을 deesserialize-.net Core C #

분류에서Dev

Eliminate weird characters in Json string when calling the API by using C#?

분류에서Dev

How can I use the get() function to turn the answer in the entry() box into a string

분류에서Dev

json-c 정의되지 않은 기호가있는 nginx 모듈 : json_object_to_json_string

분류에서Dev

Telegraf json_string_fields for string arrays

분류에서Dev

Deserialize json twitter string into object

분류에서Dev

동적 csv를 List <string> C #을 포함하는 json으로 변환

분류에서Dev

C # JSON.NET을 사용하여 Dictionary <(Enum, Enum), string> 역 직렬화

분류에서Dev

How to use the Object Detection Bounding Box in a decision condition to turn on the LED on Raspberry Pi using C++ OpenCV?

분류에서Dev

My Toshiba Satellite C55-B5319 fans will not turn on; how do I make them work?

분류에서Dev

Null termination of C string

분류에서Dev

c qsort string array

Related 관련 기사

  1. 1

    C++ turn a std::tuple<char, char, char> into a std::string?

  2. 2

    Turn comma seperated string into an array

  3. 3

    C # OpenApi Json String 응답

  4. 4

    C# JSON Rest Response String

  5. 5

    Convert dynamic csv to json containing List<string> C#

  6. 6

    How to assign JSON array to a string using the SimpleJSON library in C++

  7. 7

    JSON serialize a custom C# struct to a string, not an Object

  8. 8

    How can I turn a delimited string into a nested hashtable in PowerShell?

  9. 9

    How to turn a hclust-object into JSON for D3?

  10. 10

    JSON parseError - get json as string

  11. 11

    JSON을 C #의 Dictionary <string, List <string >>으로 역 직렬화하려고합니다.

  12. 12

    Как предотвратить инициализацию свойств, которых нет в строке JSON во время десериализации в C #?

  13. 13

    Convert String to JSON in bash

  14. 14

    Deserialise json form string

  15. 15

    List of Objects To Json String

  16. 16

    JSON error: Expecting 'STRING'

  17. 17

    Parse a JSON String and display it

  18. 18

    string, int 배열로 json을 deesserialize-.net Core C #

  19. 19

    Eliminate weird characters in Json string when calling the API by using C#?

  20. 20

    How can I use the get() function to turn the answer in the entry() box into a string

  21. 21

    json-c 정의되지 않은 기호가있는 nginx 모듈 : json_object_to_json_string

  22. 22

    Telegraf json_string_fields for string arrays

  23. 23

    Deserialize json twitter string into object

  24. 24

    동적 csv를 List <string> C #을 포함하는 json으로 변환

  25. 25

    C # JSON.NET을 사용하여 Dictionary <(Enum, Enum), string> 역 직렬화

  26. 26

    How to use the Object Detection Bounding Box in a decision condition to turn on the LED on Raspberry Pi using C++ OpenCV?

  27. 27

    My Toshiba Satellite C55-B5319 fans will not turn on; how do I make them work?

  28. 28

    Null termination of C string

  29. 29

    c qsort string array

뜨겁다태그

보관