2016-07-05 18 views
1

Json StringからJArrayを解析する必要があります。JsonストリングからのJArrayの解析?

 JObject myjson = JObject.Parse(theJson); 
     JArray nameArray = JArray.Parse(theJson);     
     _len = nameArray.Count(); 

theJsonStringは、問題は、私はデバッグするとき、私はと_len = 0 nameArrayは常にnull持っているということである以下の

"{\"0\": [-26.224264705882351, 0.67876838235294112, -38.031709558823529, 46.201555361781679], 
    \"1\": [-26.628676470588236, 2.4784007352941178, -37.377297794117645, 45.959670050709867]}" 

次のとおりです。そのために私はこのコードを持っています。 エラーを見つけるお手伝いができますか?

+3

をデシリアライズするために、このコードを使用します。 –

+0

http://stackoverflow.com/questions/26270990/how-to-parse-json-objects-with-numeric-keys-using-javascriptserializer – CodeCaster

答えて

0

あなたのJSONは有効なJSON

無効です。
{"0": [-26.224264705882351, 0.67876838235294112, -38.031709558823529, 46.201555361781679], 
    "1": [-26.628676470588236, 2.4784007352941178, -37.377297794117645, 45.959670050709867]} 

JSON配列をイマイチJSON

var myjson = JsonConvert.DeserializeObject <Dictionary<int, double[]>>(theJson); 
int _len = myjson.Count; 
2

FYI Countはメソッドではなく、プロパティです。 以下のように、このように使用する例を追加しました。

string json = @" 
    [ 
     { ""test1"" : ""desc1"" }, 
     { ""test2"" : ""desc2"" }, 
     { ""test3"" : ""desc3"" } 
    ]"; 

    JArray a = JArray.Parse(json); 
    var _len = a.Count; 

ここでは、ここでは、JArrayにあなたのJSONを解析することはできません_len = 3

0

の値を取得します。しかし、json文字列を保持したい場合は、配列のようにJsonObjectを使用することができます。ここで

は、いくつかの不正なコードですが、それはあなたにいくつかのアイデアを与えることができ、私はあなたのJSON文字列内の数字は、いくつかのIDで、Xに0を開始することを前提としています。ここ

 //Your json, the id is the value 0..1..2 
     string json = "{\"0\": [-26.224264705882351, 0.67876838235294112, -38.031709558823529, 46.201555361781679], 
         \"1\": [-26.628676470588236, 2.4784007352941178, -37.377297794117645, 45.959670050709867]}"; 

     //Create json object 
     JObject myjson = JObject.Parse(json); 

     //Get the number of different object that you want to get from this json 
     int count = getCountMyJson(myjson); 

     //Create your Jarray 
     JArray nameArray = new JArray(); 

     //Get the value from the json, each different value , start to 0 and going to the maximum value 
     for (int i = 0; i < count; i++) 
     { 
      if(myjson[i+""] != null) 
      nameArray.Add(myjson[i + ""]); 
     } 
     //Now you have a JArray that match all your json value (here the object 0 and 1) 

が悪いの関数です(不正なコード、嫌である間)が、それが動作し、あなたが()idはXXXに0であると仮定すると、それで何ができるかを理解することができます。

public static int getCountMyJson(JObject json) 
    { 
     int i = 0; 
     while(json.GetValue(i+"") != null) 
     { i++; } 
     return i; 
    }