2014-12-10 13 views
0

이것을 중복으로 표시하기 전에 JsonObject를 자체적으로 구현 했으므로 여기에 지침을 요청하는 이유가 무엇입니까?JSON 문자열에서 JSON 배열을 구문 분석 하시겠습니까?

JSON 문자열에서 하나 이상의 JSON 배열을 구문 분석하는 방법에 대해 머리를 쓰다듬어 왔습니다. 나는 다음과 같은 코드가 있습니다

class JsonObject 
{ 
    public struct JsonProperty 
    { 
     public String Name { get; set; } 
     public String Value { get; set; } 
    } 

    public Dictionary<String, JsonProperty> Properties; 

    public JsonObject() 
    { 
     Properties = new Dictionary<String, JsonProperty>(); 
    } 

    public JsonObject(String jsonString) 
    { 
     Properties = new Dictionary<String, JsonProperty>(); 
     ParseString(jsonString); 
    } 

    public JsonObject(List<String> properties) 
    { 
     Properties = new Dictionary<String, JsonProperty>(); 
     foreach (String s in properties) 
     { 
      String[] keyvaluepair = s.Split(':'); 
      JsonProperty prop = new JsonProperty(); 
      prop.Name = keyvaluepair[0]; 
      prop.Value = keyvaluepair[1]; 

      Properties.Add(prop.Name, prop); 
     } 
    } 

    public void AddProperty(String name, String value) 
    { 
     JsonProperty prop = new JsonProperty(); 
     prop.Name = name; 
     prop.Value = value; 

     Properties.Add(prop.Name, prop); 
    } 

    private void ParseString(String jsonString) 
    { 
     String[] splitByComma = jsonString.Split(','); 
     List<String[]> splitByColon = new List<String[]>(); 
     foreach (String s in splitByComma) 
     { 
      String[] split = s.Split(':'); 
      splitByColon.Add(split); 
     } 

     for (int i = 0; i < splitByColon.Count; i++) 
     { 
      for (int j = 0; j < splitByColon[i].Length; j++) 
      { 
       splitByColon[i][j] = splitByColon[i][j].Replace(",", ""); 
       splitByColon[i][j] = splitByColon[i][j].Replace("}", ""); 
       splitByColon[i][j] = splitByColon[i][j].Replace("{", ""); 
       splitByColon[i][j] = splitByColon[i][j].Replace("\"", ""); 
       splitByColon[i][j] = splitByColon[i][j].Replace("\\", ""); 
       splitByColon[i][j] = splitByColon[i][j].Replace(":", ""); 
      } 
     } 

     foreach (String[] array in splitByColon) 
     { 
      JsonProperty p = new JsonProperty(); 
      p.Name = array[0]; 
      p.Value = array[1]; 
      Properties.Add(p.Name, p); 
     } 
    } 

    public override String ToString() 
    { 
     StringBuilder sb = new StringBuilder(); 
     sb.Append("{"); 
     int count = 1; 
     foreach (KeyValuePair<String, JsonProperty> p in Properties) 
     { 
      sb.Append("\"" + p.Key.ToString() + "\""); 
      sb.Append(":" + p.Value.Value.ToString()); 
      if (count < Properties.Count) 
      { 
       sb.Append(","); 
      } 
      count++; 
     } 
     sb.Append("}"); 
     return sb.ToString(); 
    } 
} 

} 그것은 아주 잘 작동

을,하지만 난 JSON 배열에 가져 가고 싶어하면 나는 구석으로 자신을 설계했을 수 있습니다. 나는이처럼 보이는 문자열을 얻을 수 :

{"SSN":"300391-1453","LoanAmount":50000.0,"LoanDuration":"2015/02/02","CreditScore":550,"Receipients":[{"bankXML":"bankXML","bankJSON":"bankJSON","bankWeb":"bankWeb"]} 

을 그리고 나는 제대로 그 배열을 걸릴 것이다, 내가 보관 얼마나 방법으로 우둔입니다. 내 생각은 배열을 찾은 다음 배열 안의 각 객체를 JsonProperty로 구문 분석 한 다음 JsonArray에 모든 속성을 이름 값과 함께 저장합니다.

어떻게하면 좋을까요?

+2

거기인가 왜 .NET 객체와 JSON 사이를 이동하는 데 Javascript Serializer를 사용하지 않는 이유가 무엇입니까? – Hopdizzle

+0

@Hopdizzle Json 객체를 쉽게 관리 할 수 ​​있다고 느꼈습니다. 문자열에 속성을 추가하거나 제거하려고합니다. 나는 또한 출력이 serializer에서 무엇인지 제어 할 수 없었고 몇 가지 문제가 발생했습니다. – OmniOwl

+0

@Vipar 기존의 시리얼 라이저를 사용하면 Json 객체로 비 직렬화 할 수 있습니다 –

답변

0

json.org부터 시작합니다. 그 중에서도 (특히) JSON 문법이 있습니다. 그런 다음 토큰 화기을 작성하여 토큰 (예 : 공백, 숫자, 문자열, 콜론, 쉼표, 대괄호 (왼쪽/오른쪽), 중괄호 (왼쪽/오른쪽))의 스트림을 읽습니다. 등 그럼 당신은

. 원하는 문법을 인식하고 원하는 표현으로 다양한 구조를 변환하는 파서 쓰기 또는 당신은 미리 작성된 JSON 직렬화 도구의 숫자 중 하나를 사용할 수 있습니다.

관련 문제