0

I have this code and I tried to getting items from this JSON string but it failed.

I'm parsing the Json string from remote host.

package selectDB;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import org.json.simple.*;

public class selectDB
 {
  public static void main(String[] args) throws IOException, ParseException
  {
      String s = "";
      URL u = new URL("http://192.168.3.1/android/select.php");
      URLConnection c = u.openConnection();
      InputStream r = c.getInputStream();
      BufferedReader reader = new BufferedReader(new InputStreamReader(r));
      for(String line; (line = reader.readLine()) != null;)
          {
            s+=line;
          }
      System.out.println(s);
  }
}

the result is

{"result" : "true" , "messages" : [{"id":"866343023633578","latitute":"27","longitude":"31","number_phone":"01113171374"},{"id":"352168066354050","latitute":"27","longitude":"31","number_phone":"202222"},{"id":"50","latitute":"50","longitude":"100","number_phone":"50"},{"id":"110","latitute":"50","longitude":"50","number_phone":"110"},{"id":"120","latitute":"27","longitude":"31","number_phone":"120"},{"id":"130","latitute":"28","longitude":"29","number_phone":"120"},{"id":"140","latitute":"30","longitude":"40","number_phone":"140"},{"id":"800","latitute":"60","longitude":"30","number_phone":"800"},{"id":"353629054230064","latitute":"70","longitude":"80","number_phone":"120"}]}

Please help!

1
  • question not very clear Commented Jun 25, 2015 at 11:15

2 Answers 2

1

U can use the JsonReader class.

try (JsonReader in = Json.createReader(r)) {

      JsonObject jsonObject= in.readObject();
      YourObject obj = new YourObject();
      obj.setSomething(jsonObject.getString("something", null));

      // "something" is the key in the json file, null is the default
      // when "something" was not found      

} catch (JsonException | ClassCastException ex) {
      throw new BadRequestException("Invalid Json Input");
}
Sign up to request clarification or add additional context in comments.

Comments

0

you can use the Google Library GSON as well, it is easy to use and self explaining.

https://code.google.com/p/google-gson/

Gson Goals

Provide simple toJson() and fromJson() methods to convert Java objects to JSON and vice-versa Allow pre-existing unmodifiable objects to be converted to and from JSON Extensive support of Java Generics Allow custom representations for objects Support arbitrarily complex objects (with deep inheritance hierarchies and extensive use of generic types)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.