2

This is reading Json file code part.

           using (StreamReader streamReader = new StreamReader(tplFile, Encoding.GetEncoding("GB2312")))
            {
                try
                {
                    new JsonSerializer();
                    JsonTextReader reader = new JsonTextReader(streamReader);
                    JObject jObject = JObject.Load(reader);
                    JToken jToken = jObject["general"];
                    foreach (JToken current in (IEnumerable<JToken>)jToken)
                    {
                        PrintItem item = default(PrintItem);
                        string text = current.ToString();
                        if (text.Contains("arryname"))
                        {
                            string text2 = current["arryname"].ToString();
                            item.arryName = text2;
                            JToken jToken2 = current["value"];
                            using (IEnumerator<JToken> enumerator2 = ((IEnumerable<JToken>)jToken2).GetEnumerator())
                            {
                                while (enumerator2.MoveNext())
                                {
                                    JToken current2 = enumerator2.Current;
                                    PrintItem item2 = default(PrintItem);
                                    string text3 = current2.ToString();
                                    if (text3.Contains("size"))
                                    {
                                        item2.size = Common.Convert2Int(current2["size"].ToString());
                                    }
                                    if (text3.Contains("format"))
                                    {
                                        item2.format = current2["format"].ToString();
                                    }
                                    if (text3.Contains("value"))
                                    {
                                        item2.value = current2["value"].ToString();
                                    }
                                    if (text3.Contains("align"))
                                    {
                                        item2.align = Common.Convert2Int(current2["align"].ToString());
                                    }
                                    if (text3.Contains("style"))
                                    {
                                        item2.style = Common.Convert2Int(current2["style"].ToString());
                                    }
                                    if (text3.Contains("left"))
                                    {
                                        item2.left = Common.Convert2Int(current2["left"].ToString());
                                    }
                                    if (text2 == "items")
                                    {
                                        this.gtempItemList.arryname.Add(item2);
                                    }
                                    else
                                    {
                                        if (text2 == "pays")
                                        {
                                            this.gtempItemList.pays.Add(item2);
                                        }
                                    }
                                }
                                goto IL_376;
                            }
                            goto IL_27E;
                        }
                        goto IL_27E;
                    IL_376:
                        this.gtempItemList.ItemList.Add(item);
                        continue;
                    IL_27E:
                        if (text.Contains("size"))
                        {
                            item.size = Common.Convert2Int(current["size"].ToString());
                        }
                        if (text.Contains("format"))
                        {
                            item.format = current["format"].ToString();
                        }
                        if (text.Contains("value"))
                        {
                            item.value = current["value"].ToString();
                        }
                        if (text.Contains("align"))
                        {
                            item.align = Common.Convert2Int(current["align"].ToString());
                        }
                        if (text.Contains("style"))
                        {
                            item.style = Common.Convert2Int(current["style"].ToString());
                        }
                        if (text.Contains("left"))
                        {
                            item.left = Common.Convert2Int(current["left"].ToString());
                            goto IL_376;
                        }
                        goto IL_376;
                    }
                }
            }
            this.Refresh();
        }

    }

This is my Code.

And

{
    "general": {
        "arryname": "name1",
        "value": {
            "size": "10",
            "format": "L100"
        }
    }
}

it is my JSon file.

When the code string text2 = current["arryname"].ToString(); is running, the error

Cannot access child value on Newtonsoft.Json.Linq.JProperty.

occurred.

And I can't find reason.

1
  • You do new JsonSerializer(); then don't use the return. This looks wrong. Commented Oct 24, 2015 at 20:00

3 Answers 3

1

You should changed json file with array. Array key is [].

Sign up to request clarification or add additional context in comments.

Comments

1

Try casting it to JProperty instead of JToken, and then use its Name and Value properties:

foreach (JProperty current in jToken.OfType<JProperty>())
{
    PrintItem item = default(PrintItem);
    string text = current.Name;
    if (text.Contains("arryname"))
    {
        string text2 = current.Value.ToString();

Comments

0

I changed Json file as follows.

{
"general": [
    {"arryname": "items", "value": [ 
        {"size": "2", "format": "L100", "value": "value", "align": 0, "style": "0", "left":"1"},
        {"size": "3", "format": "L100", "value": "value", "align": 1, "style": "1", "left":"1"},
        {"size": "4", "format": "L100", "value": "value", "align": 2, "style": "2", "left":"1"},]
    },
    {"arryname": "pays", "value": [ 
        {"size": "2", "format": "L100", "value": "value", "align": 0, "style": "0", "left":"1"}]
    }]
}
}

current["arryname"] requires array. But in Json file, there was only value not array.

So I changed Json it take array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.