Jsonを使ったアプリを作成しています。
取得したJsonデータを格納したいのですが普通のメソッドとstaticメソッドが混在していて
An object reference is required for the non-staticと言う
エラーになってしまい困っています。
GetTextureBinary method:
private void GetTextureBinary(Texture2D texture){ if(texture == null){ Debug.Log("no data"); //ここにエラーメッセージを入れる } MakeAnalysisRequest(texture.EncodeToPNG()); } static async void MakeAnalysisRequest(byte[] byteData){ HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Add("Key", subscriptionKey); string requestParameters = "returnFaceId=true&returnFaceLandmarks=false" + "&returnFaceAttributes=age"; string uri = uriBase + "?" + requestParameters; HttpResponseMessage response; using(ByteArrayContent content = new ByteArrayContent(byteData)){ content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); response = await client.PostAsync(uri, content); string contentString = await response.Content.ReadAsStringAsync(); Debug.Log("contentString:" + contentString); Debug.Log(PlayerInfo.CreateFromJSON(contentString)); } } }
PlayerInfo.cs:
using UnityEngine; [System.Serializable] public class PlayerInfo{ public string faceId = "12345-678"; public FaceRactangle faceRactangle; public FaceAttributes faceAttributes; public static PlayerInfo CreateFromJSON(string jsonString){ return JsonUtility.FromJson<PlayerInfo>(jsonString); Debug.Log("playerInfo2.faceId:" + faceId); Debug.Log("playerInfo2.faceRactangle.top:" + playerInfo2.faceRactangle.top); Debug.Log("playerInfo2.faceRactangle.left:" + playerInfo2.faceRactangle.left); Debug.Log("playerInfo2.faceRactangle.width:" + playerInfo2.faceRactangle.width); Debug.Log("playerInfo2.faceRactangle.height:" + playerInfo2.faceRactangle.height); Debug.Log("playerInfo2.faceAttributes:" + playerInfo2.faceAttributes.age); } } [System.Serializable] public class FaceRactangle{ public int top = 10; public int left = 20; public int width = 30; public int height = 30; } [System.Serializable] public class FaceAttributes{ public int age = 55; }
errorメッセージ
ArgumentException: JSON must represent an object type. UnityEngine.JsonUtility.FromJson (System.String json, System.Type type) (at /Users/builduser/buildslave/unity/build/Modules/JSONSerialize/Public/JsonUtility.bindings.cs:50) UnityEngine.JsonUtility.FromJson[T] (System.String json) (at /Users/builduser/buildslave/unity/build/Modules/JSONSerialize/Public/JsonUtility.bindings.cs:33) PlayerInfo.CreateFromJSON (System.String jsonString) (at Assets/Scripts/PlayerInfo.cs:11) OpenCVForUnityExample.FaceDetectionWebCamTexture+<MakeAnalysisRequest>d__22.MoveNext () (at Assets/Scripts/FaceDetectionWebCamTexture.cs:220) --- End of stack trace from previous location where exception was thrown --- System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () (at <>:0) System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__6_0 (System.Object state) (at <>:0) UnityEngine.UnitySynchronizationContext+WorkRequest.Invoke () (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnitySynchronizationContext.cs:111) UnityEngine.UnitySynchronizationContext.Exec () (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnitySynchronizationContext.cs:72) UnityEngine.UnitySynchronizationContext.ExecuteTasks () (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnitySynchronizationContext.cs:93)
格納したいJson文字列データ
[{"faceId":"12345-678","faceRectangle":{"top":10,"left":20,"width":30,"height":30},"faceAttributes":{"age":55.0}}]
ネットで半日ほど調べたのですが同じような事例は(英語で)いくつかあるのですが
正しい答えを見つけることができません。どこをどのように修正したらいいのか分かる方教えて下さい。
環境)
PC: mac
Unity2018.2
言語:C#
回答4件
あなたの回答
tips
プレビュー