- Home /
Parse JSON with LitJSON
I would like to parse a JSON using LitJSON. My JSON looks like :
{
"title_roof": "TILESen",
"detailsRed": "Classic red",
"detailsRedShaded": "Shaded red",
"detailsGreen": "Forest green",
"detailsGreenShaded": "Shaded green",
"detailsTerracota": "Terracotta",
"detailsBrownShaded": "Shaded brown",
"detailsBlack": "Black ",
"title_wall": "WALLS",
"wall1": {
"name": "wall_name",
"normal_texture": "normal.png",
"hover_texture": "hover.png",
"wall_texture": "texture.png"
},
"wall2": {
"name": "wall_name_2",
"normal_texture": "normal2.png",
"hover_texture": "hover2.png",
"wall_texture": "texture2.png"
},
"tile1": {
"name": "tile_name",
"normal_texture": "normal.png",
"hover_texture": "hover.png",
"details_texture": "details.png",
"roof_texture": "roof.png",
"ridge_color": "001144"
}
}
I tried using this idea but i am geeting error like:
Expression denotes a `type', where a "variable", "value" or "method group" was expected
when i use :
using UnityEngine;
using System.Collections;
using LitJson;
public class Loadr : MonoBehaviour {
string url= "http://domain/site/get_texts.php"; //this is where i get my json
void Start (){
WWW www = new WWW(url);
print(www.text);
var a = new LitJson.JsonReader(www.text);
print(a.ToString());
}
}
How can i parse the json to access all the walls or all the tiles (in reality there will be more than then one) and access "name" property for e.g. ??
Are you joking? The question you've linked is not a tutorial. Someone else also had a problem to use it because he used it wrong.
Have you even read the answer to the question?
sorry about that. i change it to "idea" :-D. I saw the answer and i didn't succeeded.
Answer by rutter · Jun 08, 2012 at 04:52 PM
This line:
Object a = LitJson.JsonReader(www.text);
That's a constructor call, yes? If so, you'll need to use the new
keyword:
Object a = new LitJson.JsonReader(www.text);
Also, you might want to store the reference as something other than an Object
. You may get a compiler error if the system thinks you meant UnityEngine.Object
as opposed to System.Object
.
What should i write ins$$anonymous$$d of Object ? LitJSON.JsonReader ?
Yes, or just "var". That will automatically type the variable as LitJSON.JsonReader due to type inference.
Answer by podperson · Dec 29, 2012 at 09:36 PM
Try the newly improved JSONParse:
http://wiki.unity3d.com/index.php?title=Head_First_into_Unity_with_UnityScript
To parse the data you'd write:
var data : json = json.fromString( your_JSON );
to grab stuff out of it you use:
data._get( key ) (and it chains, so you can data._get("wall1")._get("_name").string_value;
Everything is nested json objects (see the code for further documentation).
I've been able to add a certain amount of syntax sugar for generating new json, but it's harder to make it work the other way.
You should at least add a disclaimer that you're the developer...
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
jSON data error Unity3d 0 Answers
LitJSON android problem 1 Answer
How to get object array from json? 0 Answers