2016-09-19 14 views
4

JSONを解析しようとすると非常に奇妙なエラーが発生します。 が効果、ファイルは以下のように非常に簡単で単純なオブジェクトから構成されている:JSONを解析する際の問題

public static boolean isRegistered(int nmb) { 
    boolean toReturn = true; 
    JsonReader reader = null; 
    try { 
     reader = new JsonReader(new InputStreamReader(new URL("xxx").openConnection().getInputStream())); 
     reader.beginObject(); 
     while(reader.hasNext()) { 
      String name = reader.nextName(); 
      Log.i("Next value", name); 
      switch (name) { 
       case "registered": 
        toReturn = reader.nextBoolean(); 
        break; 
       case "firstname": 
        ProfileManager.getInstance().setFirstname(reader.nextString()); 
        break; 
       case "name": 
        ProfileManager.getInstance().setName(reader.nextString()); 
        break; 
       case "email": 
        break; 
       case "picture": 
        break; 
       case "username": 
        break; 
      } 
     } 
     reader.endObject(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      reader.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    return toReturn; 
} 

{ 
    "registered":false, 
    "firstname":"xxx", 
    "name":"yyyy", 
    "email":"[email protected]", 
    "picture":"xxxxx.jpg", 
    "username":"xxxy" 
} 

このファイルを解析するために、私は、Android SDKの例に触発されて、次のコードを使用しました

実行を開始すると、実行時にエラーが発生します。

String name = reader.nextName(); 

エラーは、名前は期待されていますが、STRINGを取得しています。確かに、nextString()をnextString()に置き換え、反対のエラーを取得しました。文字列が必要ですがNAMEでした。私はpeek()メソッドのおかげで最初の値を調べることにしました。そして、最初の要素がNAMEであることがはっきりと分かります。だから私はループを使わずに手動でオブジェクトを読むことで非常に単純なことを試みました。どのように可能ですか?さらに、このコードを実行可能にするためには何を修正する必要がありますか?

ありがとうございました!

+2

を提供作り付けのライブラリを使用してみては? – mariozawa

+0

答えはありません。 1つの提案http://stackoverflow.com/a/18998203/3049065 org.json library ..を使用しています。 –

+0

私はあなたの 'switch'(デフォルトを含む)のすべてのケースに対して適切な処理を追加したいと思います。現在の実装では、「メール」に遭遇した後に例外がスローされることが予想されます –

答えて

5

はGSONを使用しないのはなぜAndroidのSDKで

   JSONObject obj = new JSONObject(jsonString); 
       boolean registered = obj.getBoolean("registered"); 
       String firstname = obj.getString("firstname"); 
       String name = obj.getString("name"); 
       String email = obj.getString("email"); 
       String picture = obj.getString("picture"); 
       String username = obj.getString("username"); 
+0

完璧に動作します!ありがとう:) – user1382272

+0

@ user1382272あなたは大歓迎です:) –

0
public String CallUR(String url, int method) { 
    BufferedReader bufferedReader = null; 
    String result = null; 
    HttpURLConnection httpURLConnection = null; 
    /* Take an URL Object*/ 
    try { 
     URL url1 = new URL(url); 
     httpURLConnection = (HttpURLConnection) url1.openConnection(); 
     httpURLConnection.setRequestMethod("GET"); 
     httpURLConnection.setConnectTimeout(20000); 
     httpURLConnection.connect(); 

     InputStream inputStream = httpURLConnection.getInputStream(); 
     StringBuffer stringBuffer = new StringBuffer(); 

     if (inputStream == null) { 
      return null; 
     } 

     bufferedReader = new BufferedReader(new    InputStreamReader(inputStream)); 
     String line = ""; 
     while ((line = bufferedReader.readLine()) != null) { 
      stringBuffer.append(line + " "); 
     } 

     if (stringBuffer.length() == 0) { 
      return null; 
     } 
     /*Close Input Stream*/ 
     if (inputStream != null) 
      inputStream.close(); 

     result = stringBuffer.toString(); 
     return result; 
    } catch (MalformedURLException e) { 

     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     if (httpURLConnection != null) 
      httpURLConnection.disconnect(); 

     if (bufferedReader != null) 
      try { 
       bufferedReader.close(); 
      } catch (final Exception e) { 
      } 
    } 
    return result; 

} 



JSONObject jsonObject = new JSONObject(resultString); 

String registeredValue= jsonObject .getString("registered"); 
String firstnameValue= jsonObject .getString("firstname"); 
String nameValue= jsonObject .getString("name"); 
String emailValue= jsonObject .getString("email"); 
String pictureValue= jsonObject .getString("picture"); 
String usernameValue= jsonObject .getString("username"); 
0
**If you using java then ,you can create one bean** 

package com.example; 

import java.util.HashMap; 
import java.util.Map; 
import javax.annotation.Generated; 
import com.fasterxml.jackson.annotation.JsonAnyGetter; 
import com.fasterxml.jackson.annotation.JsonAnySetter; 
import com.fasterxml.jackson.annotation.JsonIgnore; 
import com.fasterxml.jackson.annotation.JsonInclude; 
import com.fasterxml.jackson.annotation.JsonProperty; 
import com.fasterxml.jackson.annotation.JsonPropertyOrder; 


public class Example { 

private Boolean registered; 

private String firstname; 

private String name; 

private String email; 

private String picture; 

private String username; 

/** 
* 
* @return 
* The registered 
*/ 

public Boolean getRegistered() { 
return registered; 
} 

/** 
* 
* @param registered 
* The registered 
*/ 
public void setRegistered(Boolean registered) { 
this.registered = registered; 
} 

/** 
* 
* @return 
* The firstname 
*/ 
public String getFirstname() { 
return firstname; 
} 

/** 
* 
* @param firstname 
* The firstname 
*/ 
public void setFirstname(String firstname) { 
this.firstname = firstname; 
} 

/** 
* 
* @return 
* The name 
*/ 
public String getName() { 
return name; 
} 

/** 
* 
* @param name 
* The name 
*/ 
public void setName(String name) { 
this.name = name; 
} 

/** 
* 
* @return 
* The email 
*/ 
public String getEmail() { 
return email; 
} 

/** 
* 
* @param email 
* The email 
*/ 
public void setEmail(String email) { 
this.email = email; 
} 

/** 
* 
* @return 
* The picture 
*/ 
public String getPicture() { 
return picture; 
} 

/** 
* 
* @param picture 
* The picture 
*/ 
public void setPicture(String picture) { 
this.picture = picture; 
} 

/** 
* 
* @return 
* The username 
*/ 
public String getUsername() { 
return username; 
} 

/** 
* 
* @param username 
* The username 
*/ 
public void setUsername(String username) { 
this.username = username; 
} 

} 

**Then after in the function you can pass the bean,Easily you can break it.** 

public void display(Example example){ 
String userName=example.getUsername(); 
... 
... 
} 
likewise you can do complete. 

If you don't want create the bean then you can directly use the JSON Object 

JSONObject obj = new JSONObject(jsonString); 

boolean registered = obj.getBoolean("registered"); 

String firstname = obj.getString("firstname"); 

String name = obj.getString("name"); 

String email = obj.getString("email"); 

String picture = obj.getString("picture"); 

String username = obj.getString("username"); 
+0

これは、JavaとAndroidの両方にも便利です... –

関連する問題