解析 JSON 到 TextInputEditText 字段

huangapple go评论81阅读模式
英文:

Parsing JSON to TextInputEditText fields

问题

当设备扫描条形码时,我需要查询SQL表格,如果存在JSON数据(确实存在),我必须将JSON解析为文本输入编辑文本字段,并将brand_name解析到下拉菜单中,实际上为用户填充字段。我不确定如何将其解析为单独的字段。有人可以帮助我吗?

这是我拥有的JSON:

"count":"50",
"lot":"100620",
"brand_name":"Example",
"variant":"ZS/N 0,75 x 60",
"net_content":"20",
"unit_of_measure":"kg",
"sscc":"086060197000151569",
"sync":"NO",
"war_group_type":"123 example"

我对Java非常陌生,我正在使用Android Studio,谢谢!

英文:

When device scans a barcode, I need to make a query to the SQL Table and if there is JSON data there (and there is) I have to parse JSON into Text Input Edit Text fields and brand_name into spinner, practically fill the fields for user. I'm not sure how to parse it into separate fields. Can anyone help me?

This is the JSON i have:

"count":"50",
"lot":"100620",
"brand_name":"Example",
"variant":"ZS/N 0,75 x 60",
"net_content":"20",
"unit_of_measure":"kg",
"sscc":"086060197000151569",
"sync":"NO",
"war_group_type":"123 example"

I'm very new to Java and I'm working in Android Studio, thank you!

答案1

得分: 1

你可以使用Gson库来序列化/反序列化JSON消息。

您需要创建一个与您期望的JSON消息具有相同结构的类。在您的情况下,类似于:

public class Foo{
    public String count, lot, brand_name, variant, net_content, unit_of_measure, sync, war_group_type;
}

然后,假设您有一个名为jsonString的字符串,其中包含完整的JSON消息,要解析它,您可以这样做:

Gson gson = new Gson();
Foo m = gson.fromJson(jsonString, Foo.class); 

之后,您应该能够独立访问每个参数:

Toast.makeText(getContext(), "Brand name is: " + m.brand_name, Toast.LENGTH_SHORT).show();

进一步的建议/注意事项:

  • 默认情况下,Gson库将类中定义的字段映射到响应中定义的JSON键。但是,您可以使用@SerializedName来指定参数的键,而不是使用字段名。一个例子是将Foo类字段定义为:

    @SerializedName("brand_name")
    String brandName;
    
  • 尽管在您的情况下似乎不适用,但请查看下面链接中如何解析列表结构。

  • 通过查看JSON结构,确保每个字段的格式是正确的。例如,"count":"50", 表示应解析为字符串,而 "count":50, 可以解析为数字(int/long/float...)。

您可以在这里找到更多信息。

英文:

You can use the Gson library to serialize/deserialize JSON messages.

You have to create a class with the same structure that you expect for the JSON message. In your case something like:

public class Foo{
    public String count, lot, brand_name, variant, net_content, unit_of_measure, sync, war_group_type;
}

Then, considering that you have the JSON full message in a String with name jsonString to parse it you do like this:

Gson gson = new Gson();
Foo m = gson.fromJson(jsonString, Foo.class); 

After that, you should be able to access each parameter independently:

Toast.makeText (getContext(), "Brand name is: " + m.brand_name, Toast.LENGTH_SHORT).show();

Further recommendations/considerations:

  • By default, the Gson library will map the fields defined in the class to the JSON keys defined in the response. However, you can use @SerializedName to specify the key for a parameter instead of using the field name. One example is to define you Foo class fields like this:

     @SerializedName('brand_name')
     String brandName;
    
  • Although it seems to not apply in your case, review how to parse list structures on link below.

  • Make sure the format for every field is correct by looking at the JSON structure. For example "count":"50", means it should be parsed as String, while "count":50, can be parsed as numeric (int/long/float...).

You can find more information here.

huangapple
  • 本文由 发表于 2020年9月22日 16:54:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64006266.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定