0

わかりましたので、私はこの文字列を持っています:

[{"id":1},{"id":2,"children":[{"id":3},{"id":4,"children":[{"id":5,"children":[{"id":6}]}]},{"id":7}]}]

それを解析して、データベースに入力する意味のあるデータとして使用できるようにしたいと考えています。

解析後の出力を配列として/またはデータを抽出する方が良いと思われる場合は、別の出力を提案できます。これは私の考えです。

[0]  id:1
[1]  id:2 -> id:3, id:4 -> id:5 -> id:6
[2]  id:7

これは正規表現でも可能ですか

なぜ私があなたにこれを尋ねているのか、あなたに理解を深めてもらうために。ここにツリー構造があります:

デモ: http://jsbin.com/UXIpAHU/3/edit

出力を解析し、それを 2 列の SQL データベースに保存できるようにしたいと考えています。

ID 列にはすべてのアイテムのすべての ID が含まれますが、親 ID を持つのは子であるか親を持つ ID だけです。そのため、SQL テーブルは DEMO に基づいて次のようになります。

ID | ParentID
------------------------
1     NULL
2     NULL
3      2
4      2
5      4
6      5
7      2

これを達成するための最良の方法は何ですか。私の問題に対するいくつかのアイデア/解決策を探しています。ありがとうございました。

4

3 に答える 3

1

それをクラスに逆シリアル化し、そこからデータを簡単に抽出できます。

System.Web.Script.Serialization.JavaScriptSerializer は System.Web.Extensions にあることに注意してください

[Serializable()]
public class Info
{
 private int _id;

 private List<Info> _children;
 public int Id {
  get { return _id; }
  set { _id = value; }
 }

 public List<Info> Children {
  get { return _children; }
  set { _children = value; }
 }

}


public void Main()
{
 string json = null;
 List<Info> result = null;
 System.Web.Script.Serialization.JavaScriptSerializer ser = new System.Web.Script.Serialization.JavaScriptSerializer();

 json = "[{\"id\":1},{\"id\":2,\"children\":[{\"id\":3},{\"id\":4,\"children\":[{\"id\":5,\"children\":[{\"id\":6}]}]},{\"id\":7}]}]";

 result = ser.Deserialize<List<Info>>(json);

 foreach (Info p in result) {
  Console.WriteLine(p.Id);

  if (p.Children != null) {
   foreach (Info c in p.Children) {
    Console.WriteLine("   " + c.Id);
   }
  }
 }

 Console.ReadLine();

}
于 2013-10-04T15:25:04.487 に答える
1

Json.NETを使用すると、文字列を渡すだけで JObject の JArray を取得できます。

JArray arr = JArray.Parse(yourString)

その後、LINQ をサポートするコレクションと同じように LINQ を使用できます。

于 2013-10-04T15:39:42.593 に答える
1

OPが質問を変更したため、以下は前の質問に基づいています。

出力を制御する場合は、転送言語として XML を使用する必要があります。使い方は非常に簡単で、C# にはサポートが組み込まれています。

結果は次のようになります。

{"id":1},{"id":2->"children":{"id":3},{"id":4->"children":{"id":5->"children":{"id":6}}},{"id":7}}

次のようになります。

<root>
    <item id="1" />
    <item id="2">
        <item id="3" />
        <item id="4">
            <item id="5">
                <item id="6" />
            </item>
        </item>
        <item id="7" />
    </item>
</root>

それからあなたはそれを読むことができます:

XElement root = XElement.Parse(xml); // or .Load(file)
Dictionary<int,int?> list = root.Descendants("item")
    .ToDictionary(x => (int)x.Attribute("id"), x => 
    {
        var parentId = x.Parent.Attribute("id");
        if (parentId == null)
            return null;
        return (int)parentId;
    });

これで、必要に応じてキーと値のペアの辞書リストができました

ID | ParentID
------------------------
1     NULL
2     NULL
3      2
4      2
5      4
6      5
7      2

=== 元に戻す ===

Dictionary<int, int?> dic = new Dictionary<int, int?>
{
    {1,null},
    {2,null},
    {3,2},
    {4,2},
    {5,4},
    {6,5},
    {7,2}
};

XElement root = new XElement("root");
foreach (var kvp in dic)
{
    XElement node = new XElement("item", new XAttribute("id", kvp.Key));

    int? parentId = kvp.Value;
    if (null == parentId)
        root.Add(node);
    else
    {
        // Get first item with id of parentId
        XElement parent = root.Descendants("item")
            .FirstOrDefault(i => (int)i.Attribute("id") == (int)parentId);
        if (null != parent) // which it shouldn't for our array
            parent.Add(node);
    }
}

文字列を取得するには、次を使用します。

string xml = root.ToString();

またはファイルに保存するには:

root.Save("filepath");
于 2013-10-04T14:54:13.007 に答える