如何使用JsonUtility统一获取子数组

时间:2016-11-15 09:38:17

标签: c# json unity3d

这是我的JSON:

{"data":[{"id":1,"layoutLabel":"Sameer Non Custom","orbNumber":"["0","1","2","3"]"},
{"id":2,"layoutLabel":"Samer Custom","orbNumber":"["2","3","4","5"]"}],"status":200}

这是我的C#类统一来反序列化它:

[System.Serializable]
class GetLayoutsResult
{
    public List<LayoutData> data;
    public int status;
}

[System.Serializable]
class LayoutData
{
    public int id;
    public string layoutLabel;
    public string [] orbNumber;
}

以下是反序列化的代码:

     GetLayoutsResult P = JsonUtility.FromJson<GetLayoutsResult>(w.text);
        if (P.status == 200)
        {
            for (int i = 0; i < P.data.Count; i++)
            {
               Debug.Log(layoutEditButtonScript.layoutName + " " + P.data[i].positionX[0])
            }
 }

我没有获得positionX数组。我得到一个索引数组错误。任何人都可以帮我解决统一的子阵列吗?

1 个答案:

答案 0 :(得分:3)

问题是您的json格式不正确。

此:

<response>
   <lst name="responseHeader">
      <int name="status">0</int>
      <int name="QTime">8</int>
   </lst>
   <lst name="initFailures" />
   <lst name="status">
      <lst name="core0">
         <str name="name">core0</str>
         <str name="instanceDir">/var/lib/solr/core0</str>
         <str name="dataDir">/var/lib/solr/core0/data/</str>
         <str name="config">solrconfig.xml</str>
         <str name="schema">schema.xml</str>
         <date name="startTime">2016-11-11T15:31:38.250Z</date>
         <long name="uptime">324812972</long>
         <lst name="index">
            <int name="numDocs">6954</int>
            <int name="maxDoc">6954</int>
            <int name="deletedDocs">0</int>
            <long name="indexHeapUsageBytes">-1</long>
            <long name="version">12</long>
            <int name="segmentCount">1</int>
            <bool name="current">true</bool>
            <bool name="hasDeletions">false</bool>
            <str name="directory">org.apache.lucene.store.NRTCachingDirectory:NRTCachingDirectory(MMapDirectory@/var/lib/solr/feature/data/index lockFactory=org.apache.lucene.store.NativeFSLockFactory@a77f582; maxCacheMB=48.0 maxMergeSizeMB=4.0)</str>
            <str name="segmentsFile">segments_3</str>
            <long name="segmentsFileSizeInBytes">165</long>
            <lst name="userData">
               <str name="commitTimeMSec">1478791558730</str>
            </lst>
            <date name="lastModified">2016-11-10T15:25:58.730Z</date>
            <long name="sizeInBytes">2605023</long>
            <str name="size">2.48 MB</str>
         </lst>
      </lst>
   </lst>
</response>

应该是这样的:

"orbNumber": "["0", "1", "2", "3"]"

无需在数组周围加上引号。

此外,您需要在模型类中将positionX重命名为orbNumber。