Tag Archives: json

Django: How to Convert Models object to JSON

1. When the models object uses “all()”:

from django.core import serializers 


data_set = models.Areas.objects.all()
res = serializers.serialize("json",data_set)

2. When the models object is a single value “get()”:

from django.forms.models import model_to_dict

obj = models.Areas.objects.get(id=value)
res = model_to_dict(obj)

3. The models object uses values or values_ List:

res={"status":True,"data":""}
obj = models.Areas.objects.values("id","name")
obj_list = list(obj)  #Need to convert to a list, otherwise it will report an error
res["data"] = obj_list
return JsonResponse(res,safe=False)

How to Solve Curl pass large JSON files Error

Solutions to errors in curl passing large JSON files

Next, the shell first obtains the token, and then sends the JSON file to the server with the token,   After hours of normal JSON files, the following shell can run normally.

#!/bin/bash
#
access_res=$(curl "http://192.168.1.1:30100/api/v1/ability_sub/external/getToken?appId=d53e9b4083a8577e31dae685305fd032&secret=27766999
0d60d4616a8ae1fd9d6fc114")

access_token=$(echo $access_res |cut -d":" -f6 |cut -d"}" -f1|sed s/\"//g)

accress_url='curl -i -X POST -H "Content-Type:application/json" -d @/opt/accesstoken/test.json http://192.168.1.1:30202/app-l59q59yp3po2
7l-store/quality/faultOrderQ?'${access_token}

echo $accress_url
$accress_url

When using curl post data, if the post data is greater than 1024 bytes, curl will not directly initiate the post request. But in two steps.

1. Send a request. The header contains an expect: 100 continue to ask whether the server is willing to accept data

2. After receiving the 100 continue response returned by the server, post the data to the server.

By default, the server is enabled to receive JSON. In this case, the query before sending is removed and – H “expect:” is added, as follows:

accress_url='curl -i -X POST -H "Expect:" -H "Content-Type:application/json" -d @/opt/accesstoken/test.json http://192.168.1.1:30202/app-l59q59yp3po2 7l-store/quality/faultOrderQ?'${access_toke

In this way, you can add scheduled tasks

How to Solve the error in JSON file of eclipse

Sometimes our back-end brother doesn’t want to write HTML and is lazy to download a front-end page. As a result, the JSON file in it always reports errors

Although it can work normally, red x looks unhappy. How to solve it

This is because eclipse thinks that JSON files do not need comments, so we can turn off the compilation check of the reported compilation errors through eclipse settings

Window → preferences → validation → JSON validator

Remove the check boxes of manual and build, and click apply and close. Just ojbk

After eclipse re checks and compiles the project, it is found that JSON no longer reports compilation exceptions

If you want to check whether the JSON file can be compiled normally, you can copy the content of the JSON file and put it into a gadget to check the JSON format. After searching a lot on the Internet, it is not recommended

 

Error reporting from objectmapper nested JSON to object

The error reported by objectmapper from nested JSON to object is because the entity class object does not have a parameterless constructor

 

1. For Java classes without constructors, the compiler will provide a default parameterless constructor; 2. If the displayed class provides a constructor, the compiler will no longer provide a default constructor; 3. When objectmapper converts a JSON string into a Java object, it needs to call the class’s parameterless constructor (assign relevant address?) 4. If a class has a parameterless constructor, it’s best to provide it with a parameterless constructor because you don’t know where it may be used

Python: json:json.decoder.JSONDecodeError: Invalid control character at: line 2 column 18 (char 19)

1. JSON. Loads (JSON_ Json.decoder.jsondecodeerror: invalid control character at: Line 2 column 18 (char 19)

reason: JSON uses rigorous format by default, which is easy to report when data is transferred across languages

solution: add the parameter strict

json.loads(json_data, strict=False)

2. JSON. Dumps (data) to convert Chinese characters into Unicode

reason: JSON performs character conversion by default

solution: add ensure_ ASCII parameter

json.dumps(data, ensure_ascii=False)

3. JSON. Loads (JSON_ Error: JSON. Decoder. Jsondecodeerror: invalid – escape: Line 1 column 89 (char 88)

Error reason: syntax error

Solutions

Check JSON_ Data data, whether it contains illegal characters, such as backslash ‘\’, change ‘\’ to ‘\ \’

When parsing background JSON data with Ajax: unexpected token o in JSON at position 1

JSON data parsing exception

When doing JSON data today, the following error occurred, saying that it was an exception to parse

VM1584:1 Uncaught SyntaxError: Unexpected token o in JSON at position 1
    at JSON.parse (<anonymous>)
    at Object.success (customer.js:170)
    at j (jquery.min.js:2)
    at Object.fireWith [as resolveWith] (jquery.min.js:2)
    at x (jquery.min.js:4)
    at XMLHttpRequest.<anonymous> (jquery.min.js:4)

The request function is as follows:

$.ajax({
        url: "../../XXX.php",
        data: {CustomerName: $("#CustomerName").val()},
        dataType: "json",
        type: "post",
        success: function(data) {
            var jsonData = JSON.parse(data);
            alert(data[0].code);
            alert(data[0].msg);
        },
        error: function() {
            alert("error!");
        },
    });

It should be noted that when using jQuery to complete Ajax requests, there is a . between Ajax and $. You can’t leave it out here. Parsing exception is because after the end of Ajax request, the JSON data transferred in the background has been automatically converted to object type. Therefore, there is no need to use JSON. Parse manual conversion here