- Home /
How to get values from JSON and makes array of GameObject
I have the 3D model of url in the JSON keyvalue then i used Boomlagoon to parsing the values from the url but when i use JSONObject it getting first data of the keyvalue only. How can i get all the values and make array of gameobject
JSON:
StartCoroutine (JsonLoad3D("http://server.com/Unity/3DTest.txt","http://server.com/Unity/3DTest.txt"));
IEnumerator JsonLoad3D ( string jsonURL, string targetID){
WWW www = new WWW (jsonURL);
yield return www;
jsonText = www.text;
JSONObject json = JSONObject.Parse(jsonText);
url=json.GetString("link");
// if(type.Equals("3d")){
Debug.Log("3D Url load inside if is "+ url);
//Check if model is available in the scene
modal = GameObject.Find (targetID);
if (!modal) { //fetch model from the URL
Debug.Log("3D !modal is "+ url);
loadingText = (GUIText)GameObject.Find ("LoadingText").GetComponent(typeof(GUIText));
loadingText.enabled = true;
var objData = ObjReader.use.ConvertFileAsync (url, true, standardMaterial);
Debug.Log("objData is "+ objData);
while (!objData.isDone) {
loadingText.text = "Loading... " + (objData.progress*100).ToString("f0") + "%";
yield return null;
}
loadingText.enabled = false;
string modelName = objData.gameObjects[0].name;
Debug.Log("modelName is "+ modelName);
modal = GameObject.Find (modelName); // find the model reference
modal.name = targetID; // change the model name to targetID !!!
}
modalClone = Instantiate( modal ) as GameObject; //clone the model
modalClone.transform.localScale += new Vector3(250,250,250);
modalClone.transform.parent = imageTarget.transform;
}
You already have a reference to the objects in objData; there's no point using GameObject.Find.
Answer by Bunny83 · Nov 18, 2014 at 05:13 AM
Well, your "data" is not valid JSON. The file contains 2 objects seperated by a comma. It would be valid if it was enclosed in square brackets "[]". In this case the parser should return an array where each element in the array contains one of the objects. So if you want to read your "data" as JSON it should be JSON. I haven't seen a parser that would parse this
If i use square brackets[], i'm getting below error
invalid json string, expecting { at 0
UnityEngine.Debug:LogError(Object)
Boomlagoon.JSON.JSONLogger:Error(String) (at Assets/JSONObject.cs:59)
Boomlagoon.JSON.JSONObject:Fail(String, Int32) (at Assets/JSONObject.cs:822)
Boomlagoon.JSON.JSONObject:Fail(Char, Int32) (at Assets/JSONObject.cs:818)
Boomlagoon.JSON.JSONObject:Parse(String) (at Assets/JSONObject.cs:438)
c__Iterator5:$$anonymous$$oveNext() (at Assets/SimpleCloudHandler.cs:127)
Can you please help me how can i get the values from JSON and display into array of objects? I'm using Boomlagoon parsing
In this case your used parser is just garbage and you should use this one :D
The parser you use always expects an object at the top level, however according to the JSON specs a JSON text can contain every valid JSON value. So starting with an array is valid.
If you want to keep the parser you use at the moment you have to structure your JSON like this:
{
"data":[
{"link":"http://server.com/Unity/3dmodel/cat/cat.obj"},
{"link":"http://server.com/Unity/3dmodel/leather_armchair/leather_armchair.obj"}
]
}
That way your file contains only 1 object. That object has a field "data" which is an array and this array contains your objects with the link.
ps: I need some sleep now. I can post an example later if i find the time ;)
I used $$anonymous$$iniJson.cs and i'm getting the url values from JSON but how can i display into array of game object as scrollview in the scene?
IDictionary search = (IDictionary) Json.Deserialize(response);
IList linksObject = (IList) search["modellink"];
foreach (IDictionary modellinksArray in linksObject) {
String modal3d=string.Format("modellinksArray: {0} ", modellinksArray["link"]);
Debug.Log("modal Url load is "+ modal3d);
}
@Bunny: I have the url values in modla3d string. Please help me, how can i create array of gameobject display in the scene?
Your answer
Follow this Question
Related Questions
Wrong object always Instantiated 1 Answer
How to make an array list set to false gradually instead of instantly? 2 Answers
I want to add cloned gameobjects that are triggered, to an array. 2 Answers
Copy Childen Of GameObjects From Array 1 To Array 2 1 Answer
Array along on Z axis and along X axis. 0 Answers