JSON 파일이 업데이트되지 않음- "널 참조에서 런타임 바인딩을 수행 할 수 없음"오류가 발생 함

자이 퍼

C #을 사용하여 JSON 파일의 값을 변경하려고하지만 값이 변경되지 않습니다. 아래는 JSON입니다. 변경하려고합니다.

{
    "client": {
        "name": "ClientName",
        "pageTitle": "PageTitle",
        "serverId": 234
     },
    "connection": {
        "router": {
            "webSocketURL": "wss://pbnasdadasdasd",
            "signalRUrl": "https://pbncrasdasdasdasd",
            "endPoint": "https://pbncasdasdadasd",
            "type": "BabelFish",
            "protocol": "WebClientGameplayProtocol.WebClientGameplayProtocolDefinition",
            "transport": [
                "webSockets"
            ]
        }

    }
}

이것이 메서드에서 내 C # 코드의 모습입니다.

string json = File.ReadAllText(Jsonfile);
            dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
            jsonObj["client"]["serverId"] = "7001";

            jsonObj["client"]["connection"]["router"]["webSocketURL"] = "wss://xrouter.program.eu";
            jsonObj["client"]["connection"]["router"]["signalRUrl"] = "https://xrouter.program.eu/h";
            jsonObj["client"]["connection"]["router"]["endPoint"] = "https://xrouter.program.eu/";

코드를 실행할 때 'NULL 참조에서 런타임 바인딩을 수행 할 수 없음'오류가 발생하고 해당 영역에 이미 값이있는 이유를 알 수 없습니다. **** 더 이상이 오류가 발생하지 않습니다. jsonObj ****

아래 오류를 참조하십시오.

여기에 이미지 설명 입력

미리 감사드립니다.

크리슈나 무 팔라

이 오류는 때문이다 jsonObj["client"]["connection"]..경우 ["client"]에 대한 나던 존재하는 connection속성. 또한 사용해보십시오JObject.Parse

string json = File.ReadAllText(Jsonfile);
var jsonObj = JObject.Parse(json);
jsonObj["client"]["serverId"] = "7001";

jsonObj["connection"]["router"]["webSocketURL"] = "wss://xrouter.program.eu";
jsonObj["connection"]["router"]["signalRUrl"] = "https://xrouter.program.eu/h";
jsonObj["connection"]["router"]["endPoint"] = "https://xrouter.program.eu/";

Console.WriteLine(jsonObj.ToString());

산출

{
  "client": {
    "name": "ClientName",
    "pageTitle": "PageTitle",
    "serverId": "7001"
  },
  "connection": {
    "router": {
      "webSocketURL": "wss://xrouter.program.eu",
      "signalRUrl": "https://xrouter.program.eu/h",
      "endPoint": "https://xrouter.program.eu/",
      "type": "BabelFish",
      "protocol": "WebClientGameplayProtocol.WebClientGameplayProtocolDefinition",
      "transport": [
        "webSockets"
      ]
    }
  }
}

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

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

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관