Unity JSON parse error

0 votes

Hi,

I have strange error when I load this JSON from storage:


KeyNotFoundException: The given key was not present in the dictionary.
System.Collections.Generic.Dictionary`2[System.String,LitJson.JsonData].get_Item (System.String key) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150)
LitJson.JsonData.get_Item (System.String prop_name) (at Assets/Best HTTP (Pro)/Examples/LitJson/JsonData.cs:253)
Ravr.Manager.RavrWallManager.onImageLoaded (UnityEngine.Texture2D imgSprite) (at Assets/_RAVR_V1/Scripts/RavrWallManager.cs:275)
LeanLoader.loadFromCache (.LLOptions options) (at Assets/Plugins/LeanLoader.cs:936)
LeanLoader.load (System.String dataURL, .LLOptions options) (at Assets/Plugins/LeanLoader.cs:901)
Ravr.Manager.RavrWallManager.LoadImg (Int32 _currentIndexImg) (at Assets/_RAVR_V1/Scripts/RavrWallManager.cs:215)
Ravr.Manager.RavrWallManager.OnLoaded () (at Assets/_RAVR_V1/Scripts/RavrWallManager.cs:186)
Ravr.Manager.RavrWallManager.ProcessMedias (System.String jsonString) (at Assets/_RAVR_V1/Scripts/RavrWallManager.cs:176)
Ravr.Manager.RavrWallManager+<GetStorageResult>c__Iterator135.MoveNext () (at Assets/_RAVR_V1/Scripts/RavrWallManager.cs:157)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)

My json was correctly loaded here is log :

jsonDoc is : {"media":[ {"id":"1", "isWtfVideo":"false", "title":"Pencils of Promise - Building a Classroom", "price":"0", "liked":"5", "isHotelVideo":"false", "image":"http://s3.amazonaws.com/ravrmedia/Ghana/img/Pencils_of_Promise_-_Building_a_Classroom.jpg", "thumb":"http://s3.amazonaws.com/ravrmedia/Ghana/img/Pencils_of_Promise_-_Building_a_Classroom.jpg", "video":"http://s3.amazonaws.com/ravrmedia/Ghana/Pencils of Promise - Building a Classroom.mp4"} ]}
UnityEngine.Debug:Log(Object)
RavrWallResponse:OnSuccess(Object) (at Assets/_RAVR_V1/Scripts/RavrWallResponse.cs:24)
com.shephertz.app42.paas.sdk.csharp.connection.RESTConnectorWWW:BuildResponseForObjectService(String, App42CallBack, String)
com.shephertz.app42.paas.sdk.csharp.connection.<Execute>d__6:MoveNext()
com.shephertz.app42.paas.sdk.csharp.connection.<WaitForRequest>d__3:MoveNext()
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)

But when he try to load next image from JSOn he give me the error 

 

private void onImageLoaded(Texture2D imgSprite)
        {
            Debug.Log("onImageLoaded");
            if (currentIndexImg < imgLoading.Length)
            {
                //Debug.Log("imgIter onloaded = " + imgIter);

                _img = GameObject.Find("Img" + imgIter);
                _txt = GameObject.Find("txt" + imgIter);
                _play = GameObject.Find("Play" + imgIter);
                _like = GameObject.Find("heart" + imgIter);
                _likeTxt = GameObject.Find("numb" + imgIter);

                // Convert loaded texture to sprite , set image
                newSprite = Sprite.Create(imgSprite as Texture2D, new Rect(0f, 0f, imgSprite.width, imgSprite.height), Vector2.zero);
                if (!_img.IsNullOrInactive())
                    _img.GetComponent<Image>().sprite = newSprite;

                if (!_txt.IsNullOrInactive())
                {
                    a = jsonMedias["media"][currentIndexImg]["title"].ToString();
                    b = a.Length;
                    if (b >= 40)
                    {
                        c = a.Substring(0, 35);
                        _txt.GetComponent<TextMeshProUGUI>().text = c + "...";
                    }
                    else
                    {
                        _txt.GetComponent<TextMeshProUGUI>().text = jsonMedias["media"][currentIndexImg]["title"].ToString();
                    }
                }

                if (!_play.IsNullOrInactive())
                {
                    _play.GetComponent<RavrCityWallPlaySelectionRadial>()._isVideoHotelsProposal = jsonMedias["media"][currentIndexImg]["isHotelVideo"].ToString();
                    _play.GetComponent<RavrCityWallPlaySelectionRadial>()._currentVideoTitle = jsonMedias["media"][currentIndexImg]["title"].ToString();
                    _play.GetComponent<RavrCityWallPlaySelectionRadial>()._currentCityName = jsonMedias["media"][currentIndexImg]["cityName"].ToString();
                    _play.GetComponent<RavrCityWallPlaySelectionRadial>()._currentVideo = jsonMedias["media"][currentIndexImg]["video"].ToString();
                }

                RavrDataManager.Instance.InfoVideo = jsonMedias["media"][currentIndexImg]["description"].ToString();

                if (!_like.IsNullOrInactive())
                {
                    if (Convert.ToInt16(jsonMedias["media"][currentIndexImg]["liked"].ToString()) != 0)
                    {
                        _like.GetComponent<Image>().sprite = heartFilled;
                        _likeTxt.GetComponent<TextMeshProUGUI>().text = jsonMedias["media"][currentIndexImg]["liked"].ToString();
                    }
                    else
                    {
                        _likeTxt.GetComponent<TextMeshProUGUI>().text = "";
                        _like.GetComponent<Image>().sprite = heartNoFilled;
                    }
                }

                if (SceneManager.GetActiveScene().name == RavrConstant.cityWallSceneName)
                {
                    if (!_price.IsNullOrInactive())
                    {
                        _price = GameObject.Find("txtprice" + imgIter);
                        _price.GetComponent<TextMeshProUGUI>().text = jsonMedias["media"][currentIndexImg]["price"].ToString();
                    }
                }
                // Increment 
                currentIndexImg++;
                imgIter++;
                ravrCarouselConstruct.InitCarousel();
                // If country as one video center it 
                if (mediaCount >= 1)
                    ravrCarouselConstruct.RotateForXImage(mediaCount);
                LoadImg(currentIndexImg); // Load next image
            }
            if(currentIndexImg == imgLoading.Length)
                StartCoroutine(FadeIn());
        }

My code work fine when I use BestHTTP pro to load JSON, all are parsed correcrly and no bug... 

Thanks,

 

asked Jul 11, 2016 in Unity by ibompuis (20 points)
recategorized Jul 11, 2016 by ibompuis

1 Answer

0 votes

After some test it's appear if in my json, values was empty like this :

 

{"media":[{"id":"1","isWtfVideo":"false","title":"Pencils of Promise - Building a Classroom","price":"0","description":"","cityName":"","isHotelVideo":"false","liked":"5","image":"http://s3.amazonaws.com/ravrmedia/Ghana/img/Pencils_of_Promise_-_Building_a_Classroom.jpg","thumb":"http://s3.amazonaws.com/ravrmedia/Ghana/img/Pencils_of_Promise_-_Building_a_Classroom.jpg","video":"http://s3.amazonaws.com/ravrmedia/Ghana/Pencils of Promise - Building a Classroom.mp4"}]}

 

"description":"","cityName":"", are no value, if I comment in my code, I have no error :

_play.GetComponent<RavrCityWallPlaySelectionRadial>()._currentCityName = jsonMedias["media"][currentIndexImg]["cityName"].ToString();

and 

RavrDataManager.Instance.InfoVideo = jsonMedias["media"][currentIndexImg]["description"].ToString();

 

Why It make a error when value is empty ? this work fine with other JSON loader 

 

Thanks

 

answered Jul 11, 2016 by ibompuis (20 points)
Ok Json returned by storage, remove Key-value when value are empty... It's a bug from backend ? How I can correct this ?
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...