1

I am working with an webapplication which recieves data from iphone and then save it in the database. Data sent by iphone is in json format. I want to parse that json object and make an object with same attributes in my we application. for that i am using fromJson(Reader json, Class<T> classOfT) function. Is it possible to make a json file in my web application and then use that file as input of web application instead of sending data from iphone app again and again. If it is then where should i create that json file and how should i access that in fromJson(Reader json, Class<T> classOfT) function.
For example:

  {
users:[{
    name: "name1",
    email: "email1",
    friends:[{
        name: "name2",
        email: "email2",
        friends:[{
            name: "name3",
            email: "email3"
        },
        {
            name: "name4",
            email: "email4"
        }]
    }]
   }]
 }

Its a json object iphone app sends. I want this object hardcoded in my webapplication so i will not need to send it again and again from iphone app. I hope now its more clear.

Thanks in advance

oliholz
  • 7,447
  • 2
  • 43
  • 82
Piscean
  • 3,069
  • 12
  • 47
  • 96
  • sorry, the question is quite unclear. Are you trying to simulate HTTP requests from your iPhone to test your web application? – Thilo Jun 14 '11 at 10:44
  • Assuming from tags assigned to your question, you are on GAE/J. In that case you should use the GAE data store to store your data, not a file. Have a look at the GAE java example, in particular this part: http://code.google.com/intl/de-DE/appengine/docs/java/gettingstarted/usingdatastore.html – Stefan Jun 14 '11 at 11:24
  • Thanks Stefan. I think i didn't explain my problem better. Please have a look on this: http://stackoverflow.com/questions/5314813/json-gson-fromjson-java-objects in this problem solution is using this line of code: gson.fromJson(this.json, User.class); I want to know whats this.json. Is it a json object or what? if it is a hard-coded json object then where should we create that object to access it with this.json? – Piscean Jun 14 '11 at 11:32
  • just one cross question, what ever JSON string you are sending, does it have a corresponding Object? In your case do u have something called `Users` object??? – M.J. Jun 14 '11 at 11:53
  • yeah i have a User object but how can i make json string. can we make a string using { users:[{ name: "name1", email: "email1"}] i think it will give an error – Piscean Jun 14 '11 at 12:26

1 Answers1

1

Your question is extremely confusing. I guess that it boils down to the following:

How can I get a Reader of a file which is included in my webapplication?

If the file is included in the public webcontent (there where your /WEB-INF folder also is and so on), then you need to use ServletContext#getResourceAsStream().

String relativeWebPath = "/file.json";
InputStream input = getServletContext().getResourceAsStream(relativeWebPath);
Reader reader = new InputStreamReader(input, "UTF-8");
// ...
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555