Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by raj231 · Nov 18, 2014 at 04:29 AM · arrayjsonarray of gameobjects

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:

[{"link":"http://server.com/Unity/3dmodel/cat/cat.obj"},{"link":"http://server.com/Unity/3dmodel/leather_armchair/leather_armchair.obj"}]

 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;
         
     }

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Nov 19, 2014 at 04:32 PM 0
Share

You already have a reference to the objects in objData; there's no point using GameObject.Find.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image raj231 · Nov 18, 2014 at 05:55 AM 0
Share

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)

avatar image raj231 · Nov 18, 2014 at 05:57 AM 0
Share

Can you please help me how can i get the values from JSON and display into array of objects? I'm using Boomlagoon parsing

avatar image Bunny83 · Nov 18, 2014 at 06:07 AM 0
Share

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 ;)

avatar image raj231 · Nov 18, 2014 at 08:07 AM 0
Share

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);

 }

avatar image raj231 · Nov 19, 2014 at 06:00 AM 0
Share

@Bunny: I have the url values in modla3d string. Please help me, how can i create array of gameobject display in the scene?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges