2016-06-20 4 views
0
{ 
query: "find a flight to go to matara to galle", 
topScoringIntent: { 
intent: "Start Activity", 
score: 0.999594033 
}, 
entities: [ 
{ 
entity: "sri lanka", 
type: "startAirport", 
startIndex: 23, 
endIndex: 28, 
score: 0.8759165 
}, 
{ 
entity: "india", 
type: "endAirport", 
startIndex: 33, 
endIndex: 37, 
score: 0.8645479 
} 
] 
} 

위의 코드에서 JObject를 사용하여 데이터를 검색하려고합니다. 그러나 예외 오류가 반환됩니다.C#에서 특수 유형 json 문자열에서 데이터를 검색하는 방법은 무엇입니까?

enter image description here

어떻게이 JSON 문자열에서 데이터를 검색 할 수 있습니까? 도와주세요. 감사.

+0

코드를 게시하십시오. – user3378165

+1

키가 인용되지 않기 때문에 이것은 실제로 유효한 JSON이 아닙니다. 데이터는 어디에서 오는가? – Shakeel

답변

2

public class TopScoringIntent 
{ 
    public string intent { get; set; } 
    public double score { get; set; } 
} 

public class Entity 
{ 
    public string entity { get; set; } 
    public string type { get; set; } 
    public int startIndex { get; set; } 
    public int endIndex { get; set; } 
    public double score { get; set; } 
} 

public class RootObject 
{ 
    public string query { get; set; } 
    public TopScoringIntent topScoringIntent { get; set; } 
    public List<Entity> entities { get; set; } 
} 

지금

JavaScriptSerializer jss = new JavaScriptSerializer(); 
RootObject obj= jss.Deserialize<RootObject>(jsonText); 

는 이제 정상적인 C#을 객체로 OBJ에 액세스 할 수 있습니다 프로젝트 내 모델 클래스 아래에 추가합니다.

0

Newtonsoft json 사용하기 다음과 같은 방법으로도 작업 할 수 있습니다. topScoringIntent 및 엔티티 근처에 개체가 중첩되어 있으므로 JObject를 사용하여 모든 JSon 데이터에 액세스 한 다음 JArray를 사용하여 액세스하는 것이 좋습니다. 배열 요소를 만들고 모델에 추가 한 다음 값을 반환하십시오.

JObject data = JObject.Parse(YourJsonData); 
    JObject topScoringIntentData = JObject.Parse(data["topScoringIntent"]); 
    JArray entitiesData = JArray.Parse(data["entities"].ToString()); 
    foreach(var item in entitiesData) 
    { 
      //access all the data in the entities 
    }; 
    //if you want all the other datas access the Jobject and stor them in your appropriate datamodel 
관련 문제